2012-11-19 144 views
3

如果我添加一个事件处理程序到组合框,SelectionChanged for examle,即时获取异常。有人可以告诉我为什么。为什么我在ComboBox中得到异常('System.NullReferenceException')

奇怪的行为,因为在我的笔记本电脑上安装了VS7与VS2010它运行成功。

异常:

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 bytes
PresentationFramework.dll!System.Windows.FrameworkTemplate.LoadTemplateXaml(System.Xaml.XamlObjectWriter objectWriter)+ 0x66 bytes PresentationFramework.dll!System.Windows.Fram eworkTemplate.LoadOptimizedTemplateContent(System.Windows.DependencyObject容器,System.Windows.Markup.IComponentConnector componentConnector,System.Windows.Markup.IStyleConnector styleConnector,System.Collections.Generic.List affectedChildren,System.Windows.UncommonField templatedNonFeChildrenField)+ 0x26a字节 PresentationFramework .dll!System.Windows.FrameworkTemplate.LoadContent(System.Windows.DependencyObject container,System.Collections.Generic.List affectedChildren)+ 0x76 bytes
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字节

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> 

回答

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}}" 

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

更可能你的处理程序的错误是错误的,它是什么?

0

我觉得问题在于你的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() 
0

我得到这个错误了。就我而言,我在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 }; 
相关问题