OnContentRendred
将启动许可证检查,如果没有有效许可证,我将提供LicenseWindow
来选择一个。如果用户在未选择有效许可证的情况下关闭LicenseWindow,MainWindow
也将关闭。
这是OnContentRendred
的处理程序 - MainWindow
的事件:
private void CheckLicenseOnContentRendered(object sender, EventArgs e)
{
ContentRendered -= CheckLicenseOnContentRendered;
try
{
Initialize(); // may throw LicenseException
}
catch (LicenseException)
{
var licenseWindow = new LicenseWindow();
if (licenseWindow.ShowDialog() == true)
{
// Do Stuff if valid License selected
}
else
{
this.Close(); // <-- NullReferenceException
}
}
catch (Exception ex)
{
// Other Exceptions will be handled here
}
}
在代码示例中标记this.Close()
会产生NullReferenceException
。
我做了一些调试,并使用ImmediateWindow我设法发现
this
不为空(应该是)
那么为什么this.Close()
可能导致NullReferenceException
,也许我如何以其他方式关闭窗口?
编辑: 堆栈跟踪:
bei Giag.DataReader.ControlLibrary.Windows.MainWindow.WindowClosing(Object sender, EventArgs e) in d:\...\Windows\MainWindow.xaml.cs:Zeile 210.
bei System.Windows.Window.OnClosing(CancelEventArgs e)
bei System.Windows.Window.WmClose()
bei System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
bei MS.Win32.UnsafeNativeMethods.UnsafeSendMessage(IntPtr hWnd, WindowMessage msg, IntPtr wParam, IntPtr lParam)
bei System.Windows.Window.InternalClose(Boolean shutdown, Boolean ignoreCancel)
bei System.Windows.Window.Close()
bei Giag.DataReader.ControlLibrary.Windows.MainWindow.CheckLicenseOnContentRendered(Object sender, EventArgs e) in d:\...\Windows\MainWindow.xaml.cs:Zeile 236.
bei System.Windows.Window.OnContentRendered(EventArgs e)
bei System.Windows.Window.<PostContentRendered>b__4(Object unused)
bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
bei System.Windows.Threading.DispatcherOperation.InvokeImpl()
bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Windows.Threading.DispatcherOperation.Invoke()
bei System.Windows.Threading.Dispatcher.ProcessQueue()
bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
bei MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
bei MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
bei System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
bei System.Windows.Threading.Dispatcher.Run()
bei System.Windows.Application.RunDispatcher(Object ignore)
bei System.Windows.Application.RunInternal(Window window)
bei System.Windows.Application.Run(Window window)
bei System.Windows.Application.Run()
bei FrameworkTestClient.App.Main() in d:\...\FrameworkTestClient\obj\Debug\App.g.cs:Zeile 0.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
编辑2:
在测试项目中,上面的代码运行正常..所以我需要在其他地方找到Fault。谢谢你的帮助,我会让你联系。
答案 0 :(得分:0)
不确定我是否正确理解了您的问题,但如果您想在未找到许可证的情况下退出整个程序,则可以使用Application.Current.Shutdown();
关闭主窗口并退出应用程序。
答案 1 :(得分:0)
Closing
事件处理程序中存在错误。
有时人们会正确学习调试...... 谢谢大家。