在WPF应用程序中处理未处理的异常的最佳方法是什么?
最佳答案
您可以使用DispatcherUnhandledException
:
XAML(App.xaml):
<Application x:Class="App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="wndMain.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException">
背后的代码(App.xaml.cs/vb:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// Handle error here
...
// Prevent default unhandled exception processing by WPF
e.Handled = true;
}
阅读more here。但是,始终始终首先进行正确数量的错误处理。不要只让错误进入这种方法。
关于wpf - 未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2251868/