10
在WPF應用程序中處理未處理的異常的最佳方法是什麼?未處理的異常
在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始終儘管做了正確數量的錯誤處理,但不要讓錯誤進入這種方法。