2010-01-05 304 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事件中放置一个断点。至少当你点击断点时,后台线程会暂停,你可能会看到哪个对象为空。

我使用的另一个蛮力方法是注释掉整个代码块,运行和测试,并逐行取消注释,直到它破坏。然后你知道至少有一个潜在的崩溃位置。