【问题标题】:Unhandled exception未处理的异常
【发布时间】:2011-01-16 03:35:35
【问题描述】:

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

【问题讨论】:

    标签: wpf exception exception-handling unhandled-exception


    【解决方案1】:

    你可以使用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。不过,始终首先进行正确数量的错误处理。不要让错误溜进这个方法。

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多