通常在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);
}

相关文章:

  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-31
  • 2021-10-07
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案