gpt4 book ai didi

c# - 使用 Message.Attach 时 Caliburn.Micro 和/或 WindowsBase 中的 NullReferenceException

转载 作者:行者123 更新时间:2023-11-30 21:03:34 25 4
gpt4 key购买 nike

现在我有一个奇怪的。这只发生在 Windows XP 下的独立构建(调试/发布,无关紧要)。在 Windows 7 下工作得很好。无论如何,因为我必须确保该应用程序在 XP 下工作,所以我不得不做一些解决方法。我不得不做 Remote 在装有 XP 的机器上进行调试,这给了我 Caliburn.Micro.dll 中“NullReferenceException”类型的第一次机会异常,然后WindowsBase.dll 中类型为“NullReferenceException”的第一次机会异常,然后崩溃并显示下面提供的日志。

无论如何,这是背景,我有一个 GridViewModel 和一个匹配的 GridView View ,它的主要部分是 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 下都运行良好。

现在我的解决方法是摆脱我的 GridView.xaml 中的 Attached 操作,转而支持代码隐藏中的良好 ol' 事件,这些事件依次调用查看模型,例如:

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

model.GridSelectionChanged(selectionChangedEventArgs);
}

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

[编辑]

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

最佳答案

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

Examples of interaction triggers

关于c# - 使用 Message.Attach 时 Caliburn.Micro 和/或 WindowsBase 中的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com