gpt4 book ai didi

.NET 4.0 WPF 自动化异常

转载 作者:行者123 更新时间:2023-12-04 08:15:19 31 4
gpt4 key购买 nike

我有一个在 .NET 3.5 SP1 中运行良好的项目。现在升级到 .NET 4.0 时发生自动化异常。

我已经在我的整个项目中搜索了与自动化相关的任何内容,但与自动化无关。此外,谷歌搜索也无助于确认这是否是一个错误。该错误仅发生在几台 PC 上,并且是随机发生的。可以完全禁用自动化,因为我认为它可能是 .NET 4.0 的错误?

Exception Source: PresentationCore
Message: Object reference not set to an instance of an object.
Stack Trace:
at System.Windows.Automation.Peers.AutomationPeer.EnsureChildren()
at System.Windows.Automation.Peers.AutomationPeer.UpdateChildrenInternal(Int32 invalidateLimit)
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdatePeer(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run()

最佳答案

EnsureChildren 方法相当简单:

private void EnsureChildren()
{
if (!this._childrenValid || this._ancestorsInvalid)
{
this._children = this.GetChildrenCore();
if (this._children != null)
{
int count = this._children.Count;
for (int i = 0; i < count; i++)
{
this._children[i]._parent = this;
this._children[i]._index = i;
this._children[i]._hwnd = this._hwnd;
}
}
this._childrenValid = true;
}
}

NullReferenceException 的唯一机会是使用 this.children[i]代码。 GetChildrenCore通常由自定义 WPF 控件的自动化对等体实现。因此,其中之一可能是在从 GetChildrenCore 返回的集合中返回 null。

如果您有任何自定义控件或任何实现自定义自动化对等项的第三方控件,那么这可能是一个可疑的对象。

您可以通过创建自定义类并覆盖 OnCreateAutomationPeer,逐个元素地禁用 UI 自动化。并返回空值。这是我所知道的禁用自动化的唯一方法。

最好的办法可能是删除 UI 的各种元素,以缩小导致问题的控件的范围。

关于.NET 4.0 WPF 自动化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6380225/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com