2011-06-17 58 views
6

我有一個在.NET 3.5 SP1中工作正常的項目。現在升級到.NET 4.0時,我發生了自動化異常。.NET 4.0 WPF自動化異常

我已搜查我的整個項目,涉及到自動化任何事情,沒有什麼做與自動化。谷歌搜索也無助於確認這是否是一個錯誤。該錯誤只發生在幾臺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() 
+0

不知道答案,但建議你繼續觀察。如果問題確實是隨機的,那麼它可能是.net 4.0的一個bug。否則它不是 – 2011-06-17 01:11:29

+0

我們遇到了同樣的問題,當我刪除了一些樣式時就離開了。當我們發現究竟是什麼原因造成的,我會在這裏寫下來。 – sinelaw 2011-08-17 14:51:42

+0

好吧,在我們的情況下,它必須使用日期時間選擇器日曆樣式的故事板動畫。我們仍然沒有調查確切原因,但我們會在解決動畫問題時刪除它們。 – sinelaw 2011-08-17 15:19:38

回答

5

的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的各種要素,以縮小其控制導致了問題。

+0

謝謝!那麼我有所有自定義控件的源代碼等似乎沒有任何東西實現自定義的自動化同行。我將嘗試覆蓋項目中所有Windows的OnCreateAutomationPeer。 – Luke 2011-06-17 01:58:15

+0

@Luke:你能以某種方式解決問題嗎?如果是這樣,請你提供一些關於你做了答案的信息嗎?我們目前陷入了同樣的問題。 – 2015-08-06 09:12:58

0

這是WPF的bug。如果沒有爲日標題行(顯示日期名稱的日曆部分)定義數據模板,則CalendarAutomationPeer.GetChildrenCore返回空值。這會導致空指針異常,通常可以在WPF中的某處處理,但有時會導致應用程序崩潰。

爲了解決這個簡單地定義DayTitleTemplate(如通過Friggers並且在該柱中How can I change the DataTemplate for DayTitleTemplate in a CalendarItemTemplate評論中提及)。