.net - 如何在PresentationFramework中调试NullReferenceException?

标签 .net wpf exception treeview nullreferenceexception

我的 .NET 4 应用程序使用 WPF TreeView,我已对其进行了增强,以启用键盘导航和延迟加载等功能。我的问题是,很少会抛出下面所示的异常。在几个月的开发过程中,这种情况只发生在我身上一次,而且我收到了一些客户的错误报告,他们也偶尔收到这种情况。这种情况非常罕见,以至于我无法更改某些内容,使用该应用程序一段时间并知道问题是否消失。

但我的主要问题是:堆栈跟踪仅显示 Microsoft 代码,其中没有我的任何内容。这是 .NET/WPF 中的错误吗?有没有机会找到根本原因?

Type: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: PresentationFramework
Stack trace:    at System.Windows.Controls.ItemContainerGenerator.ContainerFromIndex(Int32 index)
  at System.Windows.Controls.TreeViewItem.FindNextFocusableItem(Boolean walkIntoSubtree)
  at System.Windows.Controls.TreeViewItem.InternalPredictFocus(FocusNavigationDirection direction)
  at System.Windows.Controls.VirtualizingStackPanel.FocusChanged(KeyboardFocusChangedEventArgs e)
  at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
  at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
  at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
  at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
  at System.Windows.Input.InputManager.ProcessStagingArea()
  at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
  at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
  at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
  at System.Windows.UIElement.Focus()
  at System.Windows.Controls.TreeViewItem.OnItemsChanged(NotifyCollectionChangedEventArgs e)
  at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
  at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
  at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
  at System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView view)
  at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
  at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
  at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
  at System.Windows.Data.BindingExpression.Activate(Object item)
  at System.Windows.Data.BindingExpression.OnDataContextChanged(DependencyObject contextElement)
  at System.Windows.Data.BindingExpression.HandlePropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
  at System.Windows.Data.BindingExpressionBase.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
  at System.Windows.Data.BindingExpression.OnPropertyInvalidation(DependencyObject d, DependencyPropertyChangedEventArgs args)
  at System.Windows.DependentList.InvalidateDependents(DependencyObject source, DependencyPropertyChangedEventArgs sourceArgs)
  at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
  at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
  at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
  at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.RemoveAll()
  at System.Windows.Controls.VirtualizingStackPanel.ClearAllContainers(ItemsControl itemsControl)
  at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.ClearContainerForItem(DependencyObject container, Object item)
  at System.Windows.Controls.ItemContainerGenerator.UnlinkContainerFromItem(DependencyObject container, Object item)
  at System.Windows.Controls.ItemContainerGenerator.Remove(GeneratorPosition position, Int32 count, Boolean isRecycling)
  at System.Windows.Controls.VirtualizingStackPanel.CleanupRange(IList children, IItemContainerGenerator generator, Int32 startIndex, Int32 count)
  at System.Windows.Controls.VirtualizingStackPanel.CleanupContainers(Int32 firstViewport, ItemsControl itemsControl)
  at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.TreeViewItem.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.TreeViewItem.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.TreeViewItem.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.TreeViewItem.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.TreeViewItem.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ScrollContentPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.ScrollViewer.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Border.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Control.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
  at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
  at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Border.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.Controls.Control.MeasureOverride(Size constraint)
  at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
  at System.Windows.UIElement.Measure(Size availableSize)
  at System.Windows.ContextLayoutManager.UpdateLayout()
  at System.Windows.Interop.HwndSource.Process_WM_SIZE(UIElement rootUIElement, IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam)
  at System.Windows.Interop.HwndSource.LayoutFilterMessage(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)
Target: System.Windows.DependencyObject ContainerFromIndex(Int32)
Inner exception message:

注意:我的机器上也有这个,它运行完全修补的 Windows 7 x64。

最佳答案

我个人从未发现过 WPF 中的错误。然而,单步执行 WPF 代码经常使我能够将问题的根源追溯到我自己。

以下是一些有关如何启用调试框架源的 MSDN 文章:

所有版本的链接:https://msdn.microsoft.com/en-us/library/cc667410.aspx

关于.net - 如何在PresentationFramework中调试NullReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8979060/

相关文章:

c# - 实现(不处理)CollectionChanged 事件

c# - 多个异步方法,最好使用具有不同返回类型的 WhenAll

c# - 如何最好地将堆栈跟踪转换为 HTML(使用 .NET - C#)

wpf - 设置数据绑定(bind) TabControl 的样式

wpf - 使用 Caliburn micro 的动态菜单

c# - 二进制到 BigInteger 代码的 ArgumentOutOfRange 异常

php - laravel Controller 内部是否需要 try catch block ?

java - 当 Eclipse 中没有 catch 时如何观察异常?

.net - 通过 REST 在 DocumentDb 中创建文档时未经授权

c# - 使用代码将图像对象添加到 wpf