10

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

4

1 回答 1

13

您可以使用DispatcherUnhandledException

XAML(应用程序.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;
}

在这里阅读更多。不过,始终首先进行正确数量的错误处理。不要让错误溜进这个方法。

于 2010-02-12T12:55:41.503 回答