【问题标题】:WPF application crashWPF 应用程序崩溃
【发布时间】:2011-01-01 18:26:00
【问题描述】:

我正在开发一个带有 TabControl 的 WPF 应用程序(3 个带有 textBlocks、标签和 dataGrids 的选项卡)。应用程序使用后台线程自动刷新这些选项卡上的数据。一切都很好,除了如果我在其中一个后台进程触发时切换选项卡,它会引发“对象引用未设置为对象的实例”错误并使应用程序崩溃。我已经设置 DispatcherUnhandledException 来处理 UI 线程上的任何未处理的异常。除了说明错误来自 TabControl 以及可能与所选选项卡关联的对象这一事实外,堆栈跟踪没有提供太多数据。我的问题是:它们与 WPF TabControl 的固有问题是否可能导致这种行为?如前所述,我已经设置了尽可能多的错误处理,但收到的只是“对象引用...”错误。

堆栈跟踪:

在 System.Windows.Controls.ItemContainerGenerator.MoveToPosition(GeneratorPosition 位置, GeneratorDirection 方向, Boolean allowStartAtRealizedItem, GeneratorState& 状态) 在 System.Windows.Controls.ItemContainerGenerator.Generator..ctor(ItemContainerGenerator factory, GeneratorPosition 位置, GeneratorDirection 方向, Boolean allowStartAtRealizedItem) 在 System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.StartAt(GeneratorPosition 位置,GeneratorDirection 方向,布尔 allowStartAtRealizedItem) 在 System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(大小约束) 在 Microsoft.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(大小约束)在 C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataGrid\Microsoft\Windows\Controls\Primitives\DataGridRowsPresenter.cs:line 118 在 System.Windows.FrameworkElement.MeasureCore(可用大小) 在 System.Windows.UIElement.Measure(可用大小) 在 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(对象 resizedCompositionTarget) 在 System.Windows.Media.MediaContext.RenderMessageHandler(对象 resizedCompositionTarget) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔 isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象 args,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象 args,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态) 在 System.Threading.ExecutionContext.runTryCode(对象 userData) 在 System.Runtime.CompilerServices.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 wParam,IntPtr lParam,Boolean& 处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔 isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象 args,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调,对象 args,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象参数,布尔 isSingleParameter) 在 System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority 优先级,委托方法,对象 arg) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(味精和味精) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架) 在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(对象忽略) 在 System.Windows.Application.RunInternal(窗口窗口) 在 System.Windows.Application.Run(窗口窗口) 在 System.Windows.Application.Run() 在 C:\Users\jfeltis\Documents\Expression\Blend 3\Projects\salesforceConnect\salesforceConnect\obj\Debug\App.g.cs:line 0 中的 salesforceConnect.App.Main()

所有这些对我来说都是胡言乱语。比我聪明的人可以看看,希望能给我一些方向吗?谢谢。

【问题讨论】:

  • 你能在引发异常的行周围贴出代码吗?
  • 没有我只是猜测的代码,但我认为问题将归结为线程问题。
  • 你也可能有内在的例外,这可能会更清楚地说明这个主题。我的猜测是,您正在尝试更新当时不可见的控件并且 s*t 爆炸了。快速解决方案:确保它存在。更复杂的解决方案:使用可自我更新的控件(例如 ContentControl)并将其绑定到原始数​​据。
  • 我实际上找不到 DataGridRowsPresenter.cs 文件以查看第 118 行的代码。App.g.cs 文件第 0 行的代码是“#pragma checksum”..\..\ App.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "909C6872A165FE2A4169109D321954D5""。但是,我认为这不是您要寻找的。​​span>
  • 是的,当前未选中的选项卡上的数据正在更新。但是,如果我让后台进程运行几秒钟,我可以毫无问题地切换选项卡。仅当我在更新开始时尝试切换选项卡时才会发生这种情况。

标签: c# wpf tabcontrol


【解决方案1】:

尝试在您的 SelectionChanged 事件处理程序中放置一个断点,并在应用启动后立即单击不同的选项卡。您在任何选项卡中都有 UserControl 吗?如果是这样,还要在 Loaded 事件中放置一个断点。至少当你命中断点时,后台线程会暂停,你也许能看到哪个对象是空的。

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

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多