我試圖在我的 WPF 應(yīng)用程序中捕獲所有異常。我嘗試了以下代碼但它不起作用我不知道為什么?<Application x:Class="DBFilter.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" Exit="Application_Exit" DispatcherUnhandledException ="AppDispatcherUnhandledException" ><Application.Resources> </Application.Resources></Application>應(yīng)用程序.xaml.csprotected override void OnStartup(StartupEventArgs e) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledExceptionHandler); System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(AppDispatcherUnhandledException);}void AppDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ex) { Exception ex = (Exception)ea.ExceptionObject; MessageBox.Show(ex.Exception.InnerException.Message); }void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { MessageBox.Show(e.Exception.InnerException.Message); } void AppDispatcherUnhandledException(object sender,DispatcherUnhandledExceptionEventArgs e) { MessageBox.Show(e.Exception.InnerException.Message); }稍后,我會(huì)將所有異常寫(xiě)入日志表。
- 1 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報(bào)
0/150
提交
取消