4

如果我将事件处理程序添加到组合框,例如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 componentConnector, System.Windows.Markup.IStyleConnector styleConnector, System.Collections.Generic.List 受影响的Children, System.Windows.UncommonField templatedNonFeChildrenField) + 0x26a 字节 PresentationFramework.dll!System.Windows.FrameworkTemplate.LoadContent(System.Windows.DependencyObject 容器, System.Collections .Generic.List affectedChildren) + 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 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 字节

xml:

<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>
4

3 回答 3

1

当我剥离 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}}"

这是我拿出来让它运行的,大部分只是因为我缺少你的代码。其中之一肯定是有问题的。

更有可能您的处理程序的内容是错误的,其中有什么?

于 2012-11-19T14:29:43.373 回答
1

我也遇到了这个错误。就我而言,我在 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 };
于 2015-04-14T17:46:02.923 回答
0

我认为问题出在 ComboBox 上的 Tag 属性->“Tag="{Binding}"”上。尝试删除它,问题应该消失。

我在其他控件中有这种奇怪的行为:网格、文本块。一切都在安装了 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()
于 2013-01-10T16:00:47.547 回答