A:增减错误处理

D:调整公共项目
This commit is contained in:
jimmy committed 2025-05-27 23:05:20 +08:00
1 parent 67889796ed
commit 2831deffe3
12 files changed
+120 -109

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
using System.Windows;
namespace UI.Shared.Wpf;
public static class GlobalExceptionHandler
{
public static void RegisterGlobalExceptionHandlers(System.Windows.Application app)
{
// UI线程异常处理
app.DispatcherUnhandledException += (sender, e) =>
{
HandleException(e.Exception, "UI线程异常");
e.Handled = true;
};
// 非UI线程异常处理
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
if (e.ExceptionObject is Exception ex)
HandleException(ex, "非UI线程异常");
};
// 任务未观察到的异常处理
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
HandleException(e.Exception, "任务未观察异常");
e.SetObserved();
};
}
private static void HandleException(Exception ex, string context)
{
// TODO: 替换为你自己的日志系统或上报服务
string message = $"【{context}】{ex.Message}\n{ex.StackTrace}";
MessageBox.Show(message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
// 例如 LogHelper.LogError(ex);
}
}