在APP.Xaml 中 Application 中添加事件 DispatcherUnhandledException 在该事件中处理全局异常。
<Application x:Class="APPXamlSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:APPXamlSample"
Startup="Application_Startup"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
<Application.Resources>
</Application.Resources>
</Application>
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show( $"这里是全局异常{e.Exception.Message}", "全局异常", MessageBoxButton.OK, MessageBoxImage.Warning);
e.Handled = true;
}