重构了代码
This commit is contained in:
92
App.xaml.cs
92
App.xaml.cs
@@ -23,19 +23,42 @@ namespace PMSWPF;
|
||||
public partial class App : Application
|
||||
{
|
||||
public App()
|
||||
{
|
||||
}
|
||||
|
||||
public new static App Current => (App)Application.Current;
|
||||
public IServiceProvider Services { get; set; }
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
InitializeLog();
|
||||
InitializeServices();
|
||||
InitializeDataBase();
|
||||
InitializeMenu().Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); },
|
||||
() => { MessageHelper.SendLoadMessage(LoadTypes.Menu); });
|
||||
|
||||
MainWindow = Services.GetRequiredService<MainView>();
|
||||
MainWindow.Show();
|
||||
}
|
||||
|
||||
protected override void OnExit(ExitEventArgs e)
|
||||
{
|
||||
// 应用程序退出时,确保 NLog 缓冲区被清空
|
||||
LogManager.Shutdown();
|
||||
base.OnExit(e);
|
||||
}
|
||||
|
||||
private void InitializeServices()
|
||||
{
|
||||
var container = new ServiceCollection();
|
||||
|
||||
var nlog = LogManager.Setup().LoadConfigurationFromFile("Config/nlog.config").GetCurrentClassLogger();
|
||||
|
||||
container.AddLogging(loggingBuilder =>
|
||||
{
|
||||
loggingBuilder.ClearProviders();
|
||||
loggingBuilder.SetMinimumLevel(LogLevel.Trace);
|
||||
loggingBuilder.AddNLog();
|
||||
});
|
||||
|
||||
|
||||
|
||||
container.AddSingleton<DataServices>();
|
||||
container.AddSingleton<NavgatorServices>();
|
||||
container.AddSingleton<IDialogService, DialogService>();
|
||||
@@ -59,26 +82,45 @@ public partial class App : Application
|
||||
Services.GetRequiredService<GrowlNotificationService>();
|
||||
}
|
||||
|
||||
public new static App Current => (App)Application.Current;
|
||||
public IServiceProvider Services { get; }
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
private void InitializeLog()
|
||||
{
|
||||
base.OnStartup(e);
|
||||
InitDB();
|
||||
InitMenu().Await((e) =>
|
||||
{
|
||||
NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}");
|
||||
}, () =>
|
||||
{
|
||||
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
||||
});
|
||||
LogManager.Setup().LoadConfigurationFromFile("Config/nlog.config").GetCurrentClassLogger();
|
||||
|
||||
MainWindow = Services.GetRequiredService<MainView>();
|
||||
MainWindow.Show();
|
||||
|
||||
// 捕获未处理的异常并记录
|
||||
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
|
||||
{
|
||||
var ex = args.ExceptionObject as Exception;
|
||||
if (ex != null)
|
||||
{
|
||||
// 可以使用一个专用的 Logger 来记录未处理异常
|
||||
LogManager.GetLogger("UnhandledExceptionLogger").Fatal(ex, "应用程序发生未处理的异常。");
|
||||
}
|
||||
};
|
||||
|
||||
// 捕获 Dispatcher 线程上的未处理异常 (UI 线程)
|
||||
this.DispatcherUnhandledException += (sender, args) =>
|
||||
{
|
||||
LogManager.GetLogger("DispatcherExceptionLogger").Fatal(args.Exception, "UI 线程发生未处理的异常。");
|
||||
// 标记为已处理,防止应用程序崩溃 (生产环境慎用,可能掩盖问题)
|
||||
// args.Handled = true;
|
||||
};
|
||||
|
||||
// 如果您使用 Task (异步方法) 并且没有正确 await,可能会导致异常丢失,
|
||||
// 可以通过以下方式捕获 Task 中的异常。
|
||||
TaskScheduler.UnobservedTaskException += (sender, args) =>
|
||||
{
|
||||
LogManager.GetLogger("UnobservedTaskExceptionLogger").Fatal(args.Exception, "异步任务发生未观察到的异常。");
|
||||
// args.SetObserved(); // 标记为已观察,防止进程终止
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async Task InitMenu()
|
||||
/// <summary>
|
||||
/// 初始化菜单
|
||||
/// </summary>
|
||||
private async Task InitializeMenu()
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
@@ -91,7 +133,7 @@ public partial class App : Application
|
||||
{ Name = "数据转换", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.ChromeSwitch.Glyph, ParentId = 0 };
|
||||
var mqttMenu = new DbMenu()
|
||||
{ Name = "Mqtt服务器", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Cloud.Glyph, ParentId = 0 };
|
||||
|
||||
|
||||
var settingMenu = new DbMenu()
|
||||
{ Name = "设置", Type = MenuType.MainMenu, Icon = SegoeFluentIcons.Settings.Glyph, ParentId = 0 };
|
||||
var aboutMenu = new DbMenu()
|
||||
@@ -114,7 +156,7 @@ public partial class App : Application
|
||||
}
|
||||
}
|
||||
|
||||
private void InitDB()
|
||||
private void InitializeDataBase()
|
||||
{
|
||||
var _db = DbContext.GetInstance();
|
||||
_db.DbMaintenance.CreateDatabase();
|
||||
@@ -122,8 +164,8 @@ public partial class App : Application
|
||||
_db.CodeFirst.InitTables<DbNlog>();
|
||||
_db.CodeFirst.InitTables<DbDevice>();
|
||||
_db.CodeFirst.InitTables<DbVariableTable>();
|
||||
_db.CodeFirst.InitTables<DbDataVariable>();
|
||||
_db.CodeFirst.InitTables<DbS7DataVariable>();
|
||||
_db.CodeFirst.InitTables<DbVariableData>();
|
||||
_db.CodeFirst.InitTables<DbVariableS7Data>();
|
||||
_db.CodeFirst.InitTables<DbUser>();
|
||||
_db.CodeFirst.InitTables<DbMqtt>();
|
||||
_db.CodeFirst.InitTables<DbMenu>();
|
||||
|
||||
Reference in New Issue
Block a user