| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Avalonia;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Markup.Xaml;
- using Avalonia.Markup.Xaml.Styling;
- using YZWater.Avalonia.Controls;
- using YZWater.Avalonia.Views;
- using YZWater.Core.Services;
- namespace YZWater.Avalonia;
- public partial class App : Application
- {
- private ResourceInclude? _darkTheme;
- private ResourceInclude? _lightTheme;
- public override void Initialize()
- {
- AvaloniaXamlLoader.Load(this);
- _darkTheme = new ResourceInclude(new Uri("avares://YZWater.Avalonia/Themes/IndustrialTheme.axaml"))
- { Source = new Uri("avares://YZWater.Avalonia/Themes/IndustrialTheme.axaml") };
- _lightTheme = new ResourceInclude(new Uri("avares://YZWater.Avalonia/Themes/IndustrialThemeLight.axaml"))
- { Source = new Uri("avares://YZWater.Avalonia/Themes/IndustrialThemeLight.axaml") };
- ThemeService.Instance.ThemeChanged += ApplyTheme;
- // 鍏ㄥ眬寮傚父澶勭悊
- AppDomain.CurrentDomain.UnhandledException += (s, e) =>
- {
- Serilog.Log.Fatal(e.ExceptionObject as Exception, "鏈鐞嗙殑寮傚父");
- };
- TaskScheduler.UnobservedTaskException += (s, e) =>
- {
- Serilog.Log.Error(e.Exception, "鏈瀵熺殑浠诲姟寮傚父");
- e.SetObserved();
- };
- }
- public override void OnFrameworkInitializationCompleted()
- {
- // 鍒濆鍖栨湇鍔
- DatabaseService.Initialize();
- PlcService.Initialize();
- // 鍔犺浇涓婚鍜岃瑷鍋忓ソ
- ThemeService.Instance.LoadFromConfig();
- LanguageService.Instance.LoadFromConfig();
- // 搴旂敤褰撳墠涓婚
- ThemeHelper.SetTheme(ThemeService.Instance.IsDarkTheme);
- // 鍒濆鍖栬疆璇㈠拰鎶ヨ鏈嶅姟
- PlcPollingService.Instance.Start();
- AlarmService.Instance.Start();
- // 鍚姩鏁版嵁鏃ュ織
- var dataLogger = new DataLoggingService(PlcPollingService.Instance);
- dataLogger.Start();
- if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- desktop.MainWindow = new MainWindow();
- // 绐楀彛鍏抽棴鏃舵竻鐞
- desktop.ShutdownRequested += (_, _) =>
- {
- PlcPollingService.Instance.Dispose();
- AlarmService.Instance.Dispose();
- dataLogger.Dispose();
- PlcService.Dispose();
- DatabaseService.Dispose();
- };
- }
- base.OnFrameworkInitializationCompleted();
- }
- private void ApplyTheme()
- {
- var resources = Resources.MergedDictionaries;
- if (resources.Count == 0) return;
- var targetTheme = ThemeService.Instance.IsDarkTheme ? _darkTheme : _lightTheme;
- if (targetTheme == null) return;
- resources.Clear();
- resources.Add(targetTheme);
- ThemeHelper.SetTheme(ThemeService.Instance.IsDarkTheme);
- }
- }
|