.NET 4.0 WPF Automation Exception

I have a project that works fine in .NET 3.5 SP1. Now, when upgrading to .NET 4.0, I had an automation exception.

I searched my entire project for everything related to Automation, and there is nothing to do with Automation. Also, a Google search does not help confirming whether this is a mistake. The error occurs only on a few PCs, and this happens randomly. Can I turn off automation completely since I think it might be a .NET 4.0 bug?

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() 
+6
source share
3 answers

The EnsureChildren method is pretty simple:

 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; } } 

The only chance to throw a NullReferenceException is with this.children[i] code. GetChildrenCore is typically implemented by peer automation for custom WPF controls. Thus, one such chance is to return null in the collection returned from GetChildrenCore.

If you have any user controls or any third party controls that implement a custom automation peer, this will be a likely suspect.

You can turn off UI automation in stages by creating your own class and overriding OnCreateAutomationPeer and returning null. This is the only way to turn off automation.

Your best bet would be to remove the various elements of your user interface in order to narrow down which control is causing the problem.

+5
source

I had a similar problem and the following thread helped me: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/0ee9954d-0df5-4d61-8dc9-eb50c7a5be99 .

Change the DataTemplate from "DayTitleTemplate" to "{x: Static CalendarItem.DayTitleTemplateResourceKey}"

+1
source

This is a WPF error. CalendarAutomationPeer.GetChildrenCore returns zeros if there is no data template defined for the daily header line (that part of the calendar that shows the name of the days). This throws null-pointer exceptions that are usually handled somewhere in WPF, but sometimes cause the application to crash.

To solve this problem, simply define a DayTitleTemplate (as mentioned in the comment by Frigger and in this post How can I change the DataTemplate for DayTitleTemplate in CalendarItemTemplate ).

0
source

Source: https://habr.com/ru/post/890722/


All Articles