解决WPF中异常导致的程序Crash
通常在WPF中出现异常,会导致程序Crash,即使把异常Throw出来,依旧会报错,解决方法只需在App.xaml.cs中进行处理即可,废话不说,代码如下:
private int exceptionCount;
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Current.DispatcherUnhandledException += Application_DispatcherUnhandledException;
}
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
//"I'm sorry, the current application occured some issues.." +
if (exceptionCount > 0) { exceptionCount = 0; e.Handled = true; return; }
MessageWindow.Show(MessageLevel.Error, e.Exception.Message);
exceptionCount++;
e.Handled = true;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = e.ExceptionObject as Exception;
MessageWindow.Show(MessageLevel.Error, ResourceHelper.GetRsByKey("LOC_Error_00002") + e.ExceptionObject);
}