未处理的异常

Mau*_*lli 10 wpf exception-handling exception unhandled-exception

在WPF应用程序中处理未处理异常的最佳方法是什么?

Kyl*_*ndo 13

你可以使用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">
Run Code Online (Sandbox Code Playgroud)

代码背后(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;
}
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多内容.总是先做正确的错误处理量.不要只是让错误进入这种方法.