2013-04-26 32 views
8

我正在與一個奇怪的NullReferenceException發生衝突,它顯然是從ItemAutomationPeer類的GetNameCore()函數中觸發的。來自PresentationFramework.dll的NullReferenceException

該例外的細節如下,但真正有趣的方面是,它不會發生在我的開發計算機運行Windows 7,或我們測試過的其他Windows 7計算機上。它只發生在我的Windows 8 Pro測試機器上。

嘗試在WPF DataGrid控件中編輯單元格時引發異常。

我一直試圖追蹤它整天沒有成功。我嘗試使用Visual Studio遠程調試進程並逐步完成代碼,但沒有任何用戶代碼似乎觸發異常。這顯然是由PresentationFramework.Dll中的一系列事件執行的,異常只是通過AppDomain冒泡並最終導致應用程序崩潰。

如果有人能想到任何可能導致這種情況的方法或解決方法,那真的會有所幫助。

Exception Type:   System.NullReferenceException 
Exception Message: Object reference not set to an instance of an object. 
Method Information: System.String GetNameCore() 
Exception Source: PresentationFramework 

Stack Trace 
    at System.Windows.Automation.Peers.ItemAutomationPeer.GetNameCore() 
    at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree() 
    at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree() 
    at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree() 
    at System.Windows.ContextLayoutManager.fireAutomationEvents() 
    at System.Windows.ContextLayoutManager.UpdateLayout() 
    at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) 
    at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork() 
    at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 
    at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) 
    at System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget) 
    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.DispatcherOperation.InvokeImpl() 
    at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    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.LegacyInvokeImpl(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.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
    at System.Windows.Threading.Dispatcher.Run() 
    at System.Windows.Application.RunDispatcher(Object ignore) 
    at System.Windows.Application.RunInternal(Window window) 
    at System.Windows.Application.Run(Window window) 
    at System.Windows.Application.Run() 
+0

當你說這種情況的Windows 8測試機上,是建立在同一臺機器上或應用程序Windows-8機器?或者它是建立在W7上,並剛從Windows-8執行?認爲在操作系統的操作系統之間有很多dll之間的錯誤匹配,並且需要一些引用在本地複製,以便在W7機器上構建時在Windows-8上運行。嘗試從Windows-8構建它,看看問題是否仍然存在。你可能會看到一個編譯錯誤,或者在完成後得到一些innerException細節,這可能表明哪個dll導致了不匹配(類似Aero對Aero2等) – Viv 2013-04-27 06:38:34

+0

這是一個有趣的想法。我沒有裝有VS的Windows 8機器,所以我現在無法測試這個假設。但是,您可能會對框架略有不同。 – 2013-04-27 19:05:12

回答

8

很多回的往復與遠程調試器後,幾乎毫無結果的在線搜索,我能夠對這個問題追查一對夫婦誤行爲ItemAutomationPeer實例。

當我遇到這個問題時,我對UI自動化以及它在WPF框架中如何支持了零知識。實際上,當我出於某種原因考慮COM互操作時,我在一段時間內追究了錯誤的問題。如果你正在閱讀這篇文章,並且不知道什麼UI自動化可能開始herehere可能會給你一個想法,就像UI自動化在WPF中所指的那樣。

在我而言,事實證明,爲什麼該應用程序崩潰的Windows 8的測試機上,然而卻是我的機器(和無數的其他計算機,它已被部署到)在工作正常的原因是在Windows 8機器運行某種UI輔助功能應用程序(或其他一些UI自動化客戶端)。只要我啓動我的Windows 7開發計算機上的講述人應用程序,我就能夠讓應用程序崩潰。

一旦我明白了根問題,我仍然無法進一步調試以找出究竟哪個控制導致了這個問題,但更多的在線閱讀似乎指向了自定義控件的總體方向,因此我開始了一個排除過程來確定哪些自定義WPF控件是有罪的。我發現了兩個自定義控件 - 一個擴展了DataGrid,另一個擴展了ListBox。

最後,我遇到的問題的解決方案是創建自定義類,以擴展ItemsControlAutomationPeer基類,並在覆蓋OnCreateAutomationPeer方法的問題的每個自定義控件上提供自定義類。

protected override AutomationPeer OnCreateAutomationPeer() 
{ 
    return new ControlSpecificCustomAutomationPeer(this); 
} 

凡ControlSpecificCustomAutomationPeer類可能看起來像這樣至少是:

public class ControlSpecificCustomAutomationPeer 
    : ItemsControlAutomationPeer 
{ 
    public ControlSpecificCustomAutomationPeer(ItemsControl owner) 
     : base(owner) 
    { 
    } 

    protected override string GetNameCore() 
    { 
     return "";       // return something meaningful here.. 
    } 

    protected override ItemAutomationPeer CreateItemAutomationPeer(object item) 
    { 
     return new CustomDummyItemAutomationPeer(item, this); 
    }    
} 

public class CustomDummyItemAutomationPeer 
    : System.Windows.Automation.Peers.ItemAutomationPeer 
{ 
    public CustomDummyItemAutomationPeer(object item, ItemsControlAutomationPeer itemsControlAutomationPeer) 
     : base(item, itemsControlAutomationPeer) 
    { 
    } 

    protected override string GetNameCore() 
    { 
     if (Item == null) 
      return "";    

     return Item.ToString() ?? ""; 
    } 

    protected override AutomationControlType GetAutomationControlTypeCore() 
    { 
     return System.Windows.Automation.Peers.AutomationControlType.Text; 
    } 

    protected override string GetClassNameCore() 
    { 
     return "Dummy"; 
    } 
} 
+0

謝謝,這是救命! – 2013-08-21 07:50:59

+0

@ NS.X。 - 我敢打賭..我知道我浪費了相當多的時間追逐這個。 – 2013-08-21 19:31:56

+0

對於它的價值,我們遇到了同樣的問題,但只有當應用程序在Win8上運行時,或者我們在Win7中打開了講述人。我們原來是各種財產製定者之間難以調試的互動。該屬性設置後,在該代碼中的某處我們將其設置爲空。在我診斷之前,我發現上述解決方案「固定」了自動化對等症狀。 – 2014-12-15 16:57:12

相關問題