如果我将事件处理程序添加到组合框(例如 SelectionChanged),我会遇到异常。谁能告诉我为什么。
奇怪的行为,因为在我的笔记本电脑上安装了带有VS2010的win7,它运行成功。
例外:
A first chance exception of type 'System.NullReferenceException' occurred in wpfapplication.exe
Additional information: Object reference not set to an instance of an object.
异常调用堆栈:
PresentationFramework.dll!System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlReader templateReader,System.Xaml.XamlObjectWriter currentWriter)+ 0x247字节
PresentationFramework.dll!System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlObjectWriter objectWriter) + 0x66 字节 PresentationFramework.dll!System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(System.Windows.DependencyObject 容器、System.Windows.Markup.IComponentConnector 组件连接器、System.Windows.Markup.IStyleConnector 样式连接器、System.Collections.Generic.ListaffectedChildren、System.Windows. UncommonField templatedNonFeChildrenField) + 0x26a 字节 PresentationFramework.dll!System.Windows.FrameworkTemplate.LoadContent(System.Windows.DependencyObject容器,System.Collections.Generic.List受影响的Children)+ 0x76字节
PresentationFramework.dll!System.Windows.StyleHelper.ApplyTemplateContent(System.Windows.UncommonField dataField、System.Windows.DependencyObject 容器、System.Windows.FrameworkElementFactory templateRoot、int lastChildIndex、System.Collections.Specialized.HybridDictionary childIndexFromChildID、System.Windows.FrameworkTemplate框架模板)+ 0xc4 字节
PresentationFramework.dll!System.Windows.FrameworkTemplate.ApplyTemplateContent(System.Windows.UncommonField templateDataField, System.Windows.FrameworkElement 容器) + 0x3b 字节
PresentationFramework.dll!System.Windows.FrameworkElement.ApplyTemplate() + 0x7b 字节
PresentationFramework.dll!System.Windows.FrameworkElement.MeasureCore(System.Windows.Size availableSize) + 0x40 字节
PresentationCore.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize) + 0x207 字节
Xaml:
<DataGrid Margin="80,0,0,0" GridLinesVisibility="None"
RowDetailsVisibilityMode="Collapsed"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"
RowStyle="{StaticResource DataGridRowStyleAlternation}"
AutoGenerateColumns="False" SelectionMode="Single"
AlternationCount="2" Background="Transparent" HeadersVisibility="Column"
x:Name="detailsDatagrid"
Initialized="scenesDetailsDataGrid_Initialized"
ItemsSource="{Binding Source={StaticResource scenesRequisitesViewSource}}"
ColumnDisplayIndexChanged="detailsDatagrid_ColumnDisplayIndexChanged" >
<DataGrid.Columns>
<DataGridTemplateColumn Header="Fahrzeug" Width="SizeToCells">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox DisplayMemberPath="Name"
ItemsSource="{Binding Path=ProjectsRequisites, Source={StaticResource projectsViewSource}, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding Path=RequisiteId,Mode=TwoWay}"
SelectedValuePath="ID"
SelectionChanged="ComboboxRequisite_SelectionChanged"
Tag="{Binding}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Costs,StringFormat={}{0:C}, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" Header="Kosten" Width="SizeToCells" />
<DataGridTextColumn Binding="{Binding Path=Count}" Header="Anzahl" Width="SizeToHeader" />
</DataGrid.Columns>
</DataGrid>
我也遇到这个错误。就我而言,我在 WinForms 应用程序中托管了一些 WPF 用户控件,并且错误发生在某些计算机上,但没有发生在其他计算机上。
此外,当我使用
AppDomain.CurrentDomain.UnhandledException
捕获异常时,我得到了比 LoadTemplateXaml
更深的堆栈跟踪:
Object reference not set to an instance of an object.
Stack:
at System.Windows.DeferredAppResourceReference.GetValue(BaseValueSourceInternal valueSource)
at System.Windows.StaticResourceExtension.TryProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference, Boolean mustReturnDeferredResourceReference)
at System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference)
at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
at System.Xaml.XamlObjectWriter.Logic_ProvideValue(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.Logic_AssignProvidedValue(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteEndMember()
at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
...
事实证明,问题是,因为我托管在 WinForms 应用程序中,所以
Application.Current
为空。我通过在创建 UserControl 之前实例化 WPF 应用程序解决了该问题:
if (Application.Current == null)
new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
当我删除 xaml 中依赖于您的背景代码的内容时,我无法重现这一点。这以及您的错误的性质向我表明,您的 xaml 所绑定的对象之一出现了问题。都初始化了吗?
ColumnDisplayIndexChanged="detailsDatagrid_ColumnDisplayIndexChanged" >
ItemsSource="{Binding Source={StaticResource scenesRequisitesViewSource}}"
ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"
RowStyle="{StaticResource DataGridRowStyleAlternation}"
Initialized="scenesDetailsDataGrid_Initialized"
Source={StaticResource projectsViewSource},
Binding="{Binding Path=Costs,StringFormat={}{0:C}, ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}"
这是我拿出来让它运行的,大部分只是因为我缺少你的代码。其中之一肯定是有问题的。
更有可能的是你的handler的内容是错误的,里面是什么?
我认为问题出在 ComboBox 上的 Tag 属性 ->“Tag="{Binding}"”。尝试将其删除,问题就会消失。
我在其他控件中也有这种奇怪的行为:Grid、TextBlock。在安装了 Visual Studio 2010 的所有计算机上一切正常,但在其他计算机上则不然。当我删除标签属性时,我的程序开始在其他机器上运行。
我的事件查看器错误是:
Application: PragmaGRP.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlReader, System.Xaml.XamlObjectWriter)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlObjectWriter)
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(System.Windows.DependencyObject, System.Windows.Markup.IComponentConnector, System.Windows.Markup.IStyleConnector, System.Collections.Generic.List`1<System.Windows.DependencyObject>, System.Windows.UncommonField`1<System.Collections.Hashtable>)
at System.Windows.FrameworkTemplate.LoadContent(System.Windows.DependencyObject, System.Collections.Generic.List`1<System.Windows.DependencyObject>)
at System.Windows.StyleHelper.ApplyTemplateContent(System.Windows.UncommonField`1<System.Collections.Specialized.HybridDictionary[]>, System.Windows.DependencyObject, System.Windows.FrameworkElementFactory, Int32, System.Collections.Specialized.HybridDictionary, System.Windows.FrameworkTemplate)
at System.Windows.FrameworkTemplate.ApplyTemplateContent(System.Windows.UncommonField`1<System.Collections.Specialized.HybridDictionary[]>, System.Windows.FrameworkElement)
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Grid.MeasureCell(Int32, Boolean)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32, System.Windows.Size, Boolean, Boolean)
at System.Windows.Controls.Grid.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Control.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Grid.MeasureCell(Int32, Boolean)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32, System.Windows.Size, Boolean, Boolean)
at System.Windows.Controls.Grid.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size)
at System.Windows.Controls.ContentPresenter.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Border.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Control.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Grid.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.Controls.Grid.MeasureOverride(System.Windows.Size)
at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size)
at System.Windows.UIElement.Measure(System.Windows.Size)
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(System.Object)
at System.Windows.Media.MediaContext+InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(System.Object)
at System.Windows.Media.MediaContext.RenderMessageHandler(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
at System.Threading.ExecutionContext.runTryCode(System.Object)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
at System.Windows.Threading.Dispatcher.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
at System.Windows.Application.RunDispatcher(System.Object)
at System.Windows.Application.RunInternal(System.Windows.Window)
at System.Windows.Application.Run(System.Windows.Window)
at PragmaGRP.App.Main()
我和你有类似的设置,在我的 DataGridTemplateColumn 中,我使用 DevExpress 的 ComboBoxEdit - 当绑定到 VM 中的 EditValueChanged 事件的命令时,一切正常,但它只是随机开始在PresentationFramework中遇到 NullReferenceException ;由于错误不是我的代码的一部分,因此很难查明此问题的确切原因。
最后,我相信这是由我调用 ICollectionView 变量的 Refresh() 方法引起的,当用户在组合框中选择一个项目时,我在处理更改后用该方法刷新数据网格。 在我注释掉这行代码后,错误似乎消失了。
//myCollectionView.Refresh();
我还不明白为什么会发生这种情况,因为当每个数据网格行中有值和空时都会发生这种情况。我重现此错误的方法是添加一些数据网格行,在组合框中选择一些值,上下滚动并等待一段时间。 (滚动或值更改部分没有任何问题)