WPF自定义错误处理对话框?

Eri*_*ith 10 c# wpf error-handling exception

我正在尝试设置我的WPF应用程序,以便在未处理异常时弹出错误对话框.在好的'WinForms中,这可以通过添加来实现

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Run Code Online (Sandbox Code Playgroud)

到您的Program.cs文件,然后在事件处理代码中显示您想要的任何对话框.在WPF中,我试过使用

app.Dispatcher.UnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
Run Code Online (Sandbox Code Playgroud)

但是,当我在错误处理自定义窗口上使用Show()时,应用程序立即转到"blahblah.exe已停止工作......"并关闭.如果我使用ShowDialog(),窗口可以使用,直到它关闭,然后相同的"...已停止工作......"对话框弹出并死亡.

在WinForms中,似乎关闭任何错误对话框将允许应用程序继续运行,具体取决于异常的严重程度.我似乎无法弄清楚如何在WPF中正确地做到这一点.

有任何想法吗?

Bot*_*000 7

您需要在EventArgs中将Handled设置为true.