如果我将一个eventhandler添加到combobox,SelectionChanged为examle,我得到一个异常。任何人都可以告诉我原因。
奇怪的行为,因为在我的笔记本电脑上安装了带有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.
异常callstack:
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.FrameworkTemplate.LoadOptimizedTemplateContent(System.Windows.DependencyObject container,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 container,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 container)+ 0x3b bytes
PresentationFramework.dll!System.Windows.FrameworkElement.ApplyTemplate()+ 0x7b bytes
PresentationFramework.dll!System.Windows.FrameworkElement.MeasureCore(System.Windows.Size availableSize)+ 0x40 bytes
PresentationCore.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize)+ 0x207 bytes
的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>
答案 0 :(得分: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}}"
这是我为了让它运行而取出的,大部分原因只是因为我缺少你的代码。其中一个必定是有问题的。
你的处理程序更有可能是错误的,它里面有什么?
答案 1 :(得分:0)
我认为问题是你的ComboBox上的Tag属性 - &gt; “变量=”{结合} “”。尝试删除它,问题应该消失。
我在其他控件中有这种奇怪的行为:Grid,TextBlock。所有安装了Visual Studio 2010的计算机上都可以使用,但在其他计算机上却没有。当我删除Tag属性时,我的程序开始在其他机器上工作。
我的事件查看器错误是:
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()
答案 2 :(得分: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 };