2010-01-05 400 views
0

我正在使用TabControl(3個帶有textBlocks,標籤和dataGrids的選項卡)的WPF應用程序。應用程序使用後臺線程自動刷新這些選項卡上的數據。除了事實上,如果在後臺進程之一觸發時切換選項卡,它會拋出「未設置爲對象實例的對象引用」錯誤並使應用程序崩潰。我設置了DispatcherUnhandledException來處理UI線程上的任何未處理的異常。除了聲明錯誤來自TabControl以及可能與選定選項卡關聯的對象之外,堆棧跟蹤不提供太多數據。我的問題是這樣的:他們固有的問題與WPF TabControl可能會導致此行爲?如前所述,我已經設置了儘可能多的錯誤處理,但我知道如何處理,但收到的只是「對象引用...」錯誤。WPF應用程序崩潰

堆棧跟蹤:

在System.Windows.Controls.ItemContainerGenerator.MoveToPosition(GeneratorPosition位置,GeneratorDirection方向,布爾allowStartAtRealizedItem,GeneratorState &狀態) 在System.Windows.Controls.ItemContainerGenerator.Generator..ctor( ItemContainerGenerator工廠,GeneratorPosition位置,GeneratorDirection方向,布爾allowStartAtRealizedItem) 在System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.StartAt(GeneratorPosition位置,GeneratorDirection方向,布爾allowStartAtRealizedItem) 在System.Windows.Controls .Virtualiz C:\ dd \ WPF_1 \ src \ wpf \ src \ ControlsPack \ WPFToolkit \ DataGrid \ Microsoft \ Windows \ Controls \ Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)中的ingStackPanel.MeasureOverride(Size constraint) 。原語\ DataGridRowsPresenter.cs:118 在System.Windows.FrameworkElement.MeasureCore(尺碼availableSize) 在System.Windows.UIElement.Measure(尺碼availableSize) 在System.Windows.ContextLayoutManager.UpdateLayout() 在System.Windows線.ContextLayoutManager.UpdateLayoutCallback(對象ARG) 在System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork() 在System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() 在System.Windows.Media.MediaContext.RenderMessageHandlerCore(對象resizedComposit (System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Boolean isSingleParameter) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(對象源,代表回調,對象指定參數時,布爾isSingleParameter,代表catchHandler) 在System.Windows.Threading.Dispatcher.WrappedInvoke(代表回調,對象指定參數時,布爾isSingleParameter,代表catchHandler) 在System.Windows.Threading.DispatcherOperation.InvokeImpl () at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.C ompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代碼,CleanupCode backoutCode,對象的UserData) 在System.Threading.ExecutionContext.RunInternal(的ExecutionContext的ExecutionContext,ContextCallback回調,對象狀態) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,對象狀態)在System.Windows.Threading.DispatcherOperation.Invoke() 在System.Windows.Threading.Dispatcher.ProcessQueue() 在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg, IntPtr的lParam的,布爾&處理) 在MS.Win32.HwndWrapper.WndProc(IntPtr的HWND,MSG的Int32,IntPtr的wParam中,IntPtr的lParam的,布爾&處理) 在MS。Win32.HwndSubclass.DispatcherCallbackOperation(對象O) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回調,對象指定參數時,布爾isSingleParameter) 在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(對象源,代表回調,對象ARGS ,布爾isSingleParameter,Delegate的catchHandler) at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback,Object args,Boolean isSingleParameter,Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Boolean isSingleParameter) at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority,Delegate method,Object arg) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,In TPTR LPARAM) 在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG & MSG) 在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame幀) 在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame幀) 在系統.Windows.Threading.Dispatcher.Run() 在System.Windows.Application.RunDispatcher(對象忽略) 在System.Windows.Application.RunInternal(窗口窗口) 在System.Windows.Application.Run(窗口窗口) 在System.Windows.Application.Run() at salesforceConnect.App.Main()in C:\ Users \ jfeltis \ Documents \ Expression \ Blend 3 \ Projects \ salesforceConnect \ salesforceConnect \ obj \ Debug \ App.g.cs:第0行

這一切對我來說都是胡言亂語。有人能比我自己更聰明一點,希望能給我一些指導嗎?謝謝。

+1

你可以發表引發異常的代碼嗎? – ChrisF 2010-01-05 18:37:03

+0

沒有代碼我只是猜測,但我認爲這個問題將歸結爲線程問題。 – ChrisF 2010-01-05 18:37:57

+0

你也可能有內在的例外,這可能會更多地闡明這個問題。我的猜測是,你正在嘗試更新當時不可見的控件,並且s * t爆炸。快速解決方案:確保它存在。更復雜的解決方案:使用更新自身的控件(例如ContentControl)並將其綁定到原始數​​據。 – Will 2010-01-05 18:45:19

回答

0

嘗試在您的SelectionChanged事件處理程序中放置斷點,並在應用程序啓動後立即單擊其他選項卡。你有任何標籤中的用戶控件?如果是這樣,還要在Loaded事件中放置一個斷點。至少當你點擊斷點時,後臺線程會暫停,你可能會看到哪個對象爲空。

我使用的另一個蠻力方法是註釋掉整個代碼塊,運行和測試,並逐行取消註釋,直到它破壞。然後你知道至少有一個潛在的崩潰位置。