.NET 4.0 WPF自动化异常

我有一个在.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并返回null来逐个元素地禁用UI自动化.这是我知道禁用自动化的唯一方法.

您最好的选择可能是删除UI的各种元素,以缩小导致问题的控件.

相关文章
相关标签/搜索