我有一个WPF应用程序,有时在我在Window(http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog%28v=vs.110%29.aspx)上调用ShowDialog()之后应用程序崩溃
当应用程序启动时我打开一个主对话框(它总是打开)然后我想打开一个新的子对话框(见下面的ShowModal方法),通常一切正常但有时候在调用ShowDialog()之后(见下面的代码片段)它在方法System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)中的PresentationFramework.dll中抛出System.NullReferenceException。
调用ShowDialog()之后,窗口显示在App中,然后当我单击它上面的任何地方时(例如文本框),它会抛出NullReferenceException。
我无法重现它完全随机化的崩溃模式。而且我开始变得疯狂,因为我没有任何线索如何追踪问题所在。
这是我代码中的代码段:
private Window Wnd { get; set; }
public bool ShowModal(ViewModel vm)
{
if( Wnd == null )
{
throw new InvalidOperationException( "Window was already closed" );
}
Wnd.DataContext = vm;
Result = Wnd.ShowDialog().Equals(true);
return Result;
}
Wnd不为空它包含我想要显示的窗口的实例。这一行抛出了异常
Result = Wnd.ShowDialog().Equals(true);
实际上是在Wnd.ShowDialog()
之后抛出异常快照:
System.NullReferenceException {"Object reference not set to an instance of an object."}
HResult = -2147467261
Message = Object reference not set to an instance of an object.
Data = {System.Collections.ListDictionaryInternal}
InnerExcpetion = null
Source = PresentationCore
StackTrace
at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
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.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
at System.Windows.Interop.ThreadMessageEventHandler.Invoke(MSG& msg, Boolean& handled)
at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at System.Windows.Window.ShowDialog()
at Intes.Utility.VisualizerService.UiWindow.ShowModal(ViewModel vm) in d:\Coder\Branch-AT\Source\Utilities\VisualizerService\Window abstraction\Implementation\UiWindow.cs:line 109
// this part of call stack is not important, but I will leave it
at A13.ViewModels.CalGroupExplorer.CalGroupExplorerPanelViewModel.NewMessage(IEnumerable`1 referenceIDs) in d:\Coder\Branch-AT\Source\A13\A13\ViewModels\CalGroupExplorer\CalGroupExplorerPanelViewModel.cs:line 159
at A13.ViewModels.EntryViewModel.AddReferencedMessage() in d:\Coder\Branch-AT\Source\A13\A13\ViewModels\CalGroupExplorer\EntryViewModel.cs:line 147
at A13.ViewModels.EntryViewModel.<get_AddReferencedMessageCommand>b__4(Object param) in d:\Coder\Branch-AT\Source\A13\A13\ViewModels\CalGroupExplorer\EntryViewModel.cs:line 136
at Intes.Utility.Wpf.RelayCommand.Execute(Object parameter) in d:\Coder\Branch-AT\Source\Utilities\Wpf\RelayCommand.cs:line 70
at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
at System.Windows.Controls.MenuItem.InvokeClickAfterRender(Object arg)
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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
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.LegacyInvokeImpl(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.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at System.Windows.Window.ShowDialog()
似乎是.NET Framework中的错误,我正在为项目A13和Intes使用.NET Framework 4.5。 A13是主项目,而引用了Intes(方法ShowModal在项目Intes中的类中)。 A13编译为平台目标:x86,而Intes作为平台目标:AnyCPU。
我在x64处理器上的Windows 8.1上以DEBUG模式运行应用程序(应用程序必须是x86)。
有没有人知道问题出在哪里或者有什么帮助如何追踪问题?