2

现在我有一个奇怪的。这只发生在 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 下都可以正常工作。

现在我的解决方法是摆脱AttachGridView.xaml 中的 ed 操作,转而支持代码隐藏中的良好 ol' 事件,这反过来又调用视图模型中已经准备好的方法,例如:

private void DataGridOnSelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
{
    var model = (GridViewModel) DataContext;

    model.GridSelectionChanged(selectionChangedEventArgs);
}

我知道,它看起来不太漂亮,但目前我认为这是确保该应用程序在 XP 下运行的唯一方法。知道为什么它首先会崩溃吗?

[编辑]

虽然堆栈跟踪有点令人费解,但似乎NullReferenceException确实发生了,但在我的代码的某个地方,即在动作处理程序中。这只是一个初步想法,我在其他应用程序中摆脱了同样的错误......

4

3 回答 3

2

我遇到了同样的问题,但显然你不能在同一个控件上多次使用“cal:Message.Attach”来处理两个不同的事件。您可以尝试使用交互触发器:

交互触发器示例

于 2012-10-08T09:12:58.390 回答
1

此示例向您展示如何在同一控件上处理多个事件。希望这对您有所帮助。

 <TextBox cal:Message.Attach="[Event TextChanged] = [Action DoAnything]; [Event KeyDown] = [Action DoAnything2]"/>
于 2012-10-08T13:26:24.560 回答
1

好吧,事实证明在动作处理程序的发布版本下有一个奇怪的比赛,毕竟我的错误,与 Caliburn.Micro 本身并没有真正的关系。

仍然不知道为什么它在 XP 下如此“致命”,但幸运的是我不必支持它更长时间。

于 2013-06-05T17:58:16.110 回答