我有一个在.NET 3.5 SP1中正常工作的项目。现在,当升级到.NET 4.0时,发生了自动化异常。
我已经在整个项目中搜索了与自动化相关的任何内容,而与自动化无关。此外,Google搜索无法帮助您确认是否是错误。该错误仅在几台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()
最佳答案
确保 child 的方法非常简单:
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并返回null来逐个元素禁用UI自动化。这是我知道禁用自动化的唯一方法。
最好的选择可能是删除UI的各种元素,以缩小引起问题的控件的范围。
关于.NET 4.0 WPF自动化异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6380225/