App.axaml.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Markup.Xaml.Styling;
  5. using YZWater.Avalonia.Controls;
  6. using YZWater.Avalonia.Views;
  7. using YZWater.Core.Services;
  8. namespace YZWater.Avalonia;
  9. public partial class App : Application
  10. {
  11. private ResourceInclude? _darkTheme;
  12. private ResourceInclude? _lightTheme;
  13. public override void Initialize()
  14. {
  15. AvaloniaXamlLoader.Load(this);
  16. _darkTheme = new ResourceInclude(new Uri("avares://YZWater.Avalonia/Themes/IndustrialTheme.axaml"))
  17. { Source = new Uri("avares://YZWater.Avalonia/Themes/IndustrialTheme.axaml") };
  18. _lightTheme = new ResourceInclude(new Uri("avares://YZWater.Avalonia/Themes/IndustrialThemeLight.axaml"))
  19. { Source = new Uri("avares://YZWater.Avalonia/Themes/IndustrialThemeLight.axaml") };
  20. ThemeService.Instance.ThemeChanged += ApplyTheme;
  21. // 鍏ㄥ眬寮傚父澶勭悊
  22. AppDomain.CurrentDomain.UnhandledException += (s, e) =>
  23. {
  24. Serilog.Log.Fatal(e.ExceptionObject as Exception, "鏈鐞嗙殑寮傚父");
  25. };
  26. TaskScheduler.UnobservedTaskException += (s, e) =>
  27. {
  28. Serilog.Log.Error(e.Exception, "鏈瀵熺殑浠诲姟寮傚父");
  29. e.SetObserved();
  30. };
  31. }
  32. public override void OnFrameworkInitializationCompleted()
  33. {
  34. // 鍒濆鍖栨湇鍔
  35. DatabaseService.Initialize();
  36. PlcService.Initialize();
  37. // 鍔犺浇涓婚鍜岃瑷鍋忓ソ
  38. ThemeService.Instance.LoadFromConfig();
  39. LanguageService.Instance.LoadFromConfig();
  40. // 搴旂敤褰撳墠涓婚
  41. ThemeHelper.SetTheme(ThemeService.Instance.IsDarkTheme);
  42. // 鍒濆鍖栬疆璇㈠拰鎶ヨ鏈嶅姟
  43. PlcPollingService.Instance.Start();
  44. AlarmService.Instance.Start();
  45. // 鍚姩鏁版嵁鏃ュ織
  46. var dataLogger = new DataLoggingService(PlcPollingService.Instance);
  47. dataLogger.Start();
  48. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  49. {
  50. desktop.MainWindow = new MainWindow();
  51. // 绐楀彛鍏抽棴鏃舵竻鐞
  52. desktop.ShutdownRequested += (_, _) =>
  53. {
  54. PlcPollingService.Instance.Dispose();
  55. AlarmService.Instance.Dispose();
  56. dataLogger.Dispose();
  57. PlcService.Dispose();
  58. DatabaseService.Dispose();
  59. };
  60. }
  61. base.OnFrameworkInitializationCompleted();
  62. }
  63. private void ApplyTheme()
  64. {
  65. var resources = Resources.MergedDictionaries;
  66. if (resources.Count == 0) return;
  67. var targetTheme = ThemeService.Instance.IsDarkTheme ? _darkTheme : _lightTheme;
  68. if (targetTheme == null) return;
  69. resources.Clear();
  70. resources.Add(targetTheme);
  71. ThemeHelper.SetTheme(ThemeService.Instance.IsDarkTheme);
  72. }
  73. }