现在我有一个奇怪的。这只发生在 Windows XP 的独立构建(调试/发布,无关紧要)上。在 Windows 7 下工作得非常好。无论如何,因为我必须确保应用程序在 XP 下工作,所以我不得不做一些变通方法。我不得不在一台装有 XP 的机器上进行远程调试,这给了我First chance exception of type 'NullReferenceException' in Caliburn.Micro.dll
, 然后
First chance exception of type 'NullReferenceException' in WindowsBase.dll
, 然后崩溃并出现下面提供的日志。
无论如何,这是背景,我有一个 GridViewModel 和一个匹配的 GridView 视图,它的主要部分是 WPF 的 DataGrid(因此得名),我想这就是问题所在:
<DataGrid CanUserAddRows="False" EnableRowVirtualization="True" Grid.Row="0" x:Name="DataGrid" AlternatingRowBackground="#EBEBEB" ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.CanContentScroll="True" ItemsSource="{Binding CollectionView}" IsReadOnly="True" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False"
here->> cal:Message.Attach="[Event SelectionChanged] = [Action GridSelectionChanged($eventArgs)]">
后来也在这里:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="cal:Message.Attach" Value="[Event PreviewMouseLeftButtonUp] = [Action PreviewMouseLeftButtonUp($view, $eventArgs)]"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="cal:Message.Attach" Value="[Event PreviewMouseRightButtonUp] = [Action PreviewMouseRightButtonUp($source, $eventArgs)]"/>
</Style>
</DataGrid.ItemContainerStyle>
现在在 XP 下运行应用程序并单击任何行,使用 DataGrid 的滚动条,有时甚至最大化窗口会给出以下结果:
[Exception] - Object reference not set to an instance of an object.
[Stack Trace] - at Caliburn.Micro.ActionMessage.<.cctor>b__14(ActionExecutionContext context)
at Caliburn.Micro.ActionMessage.<.cctor>b__15(ActionExecutionContext context)
at Caliburn.Micro.ActionMessage.UpdateContext()
at Caliburn.Micro.ActionMessage.ElementLoaded(Object sender, RoutedEventArgs e)
at Caliburn.Micro.View.<>c__DisplayClass3.<ExecuteOnLoad>b__0(Object s, RoutedEventArgs e)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
at MS.Internal.LoadedOrUnloadedOperation.DoWork()
at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
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)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(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)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at MyAwesomeApplication.App.Main()
现在奇怪的是,如果我在应用程序的其他部分执行以下操作:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsEnabled" Value="{Binding IsRowEnabled}"/>
<Setter Property="cal:Message.Attach" Value="[Event MouseDoubleClick] = [Action InvoiceRow()]"/>
</Style>
</ListView.ItemContainerStyle>
在 XP 和 7 下都可以正常工作。
现在我的解决方法是摆脱Attach
GridView.xaml 中的 ed 操作,转而支持代码隐藏中的良好 ol' 事件,这反过来又调用视图模型中已经准备好的方法,例如:
private void DataGridOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
{
var model = (GridViewModel) DataContext;
model.GridSelectionChanged(selectionChangedEventArgs);
}
我知道,它看起来不太漂亮,但目前我认为这是确保该应用程序在 XP 下运行的唯一方法。知道为什么它首先会崩溃吗?
[编辑]
虽然堆栈跟踪有点令人费解,但似乎NullReferenceException
确实发生了,但在我的代码的某个地方,即在动作处理程序中。这只是一个初步想法,我在其他应用程序中摆脱了同样的错误......