靻岇姢 瓴靸

鍒囨崲涓婚鏃舵竻绌哄苟閲嶆柊鍔犺浇璧勬簮瀛楀吀 - 瑙﹀彂DynamicResource鏇存柊

纾 鏇 6 鞚 鞝
攵氇
旎る皨
1dd4571a1c
1臧滌潣 氤瓴诫悳 韺岇澕8臧滌潣 於旉皜鞛戩梾 攴鸽Μ瓿 11臧滌潣 韺岇澕鞚 靷牅
  1. 8 11
      src/YZWater.Avalonia/App.axaml.cs

+ 8 - 11
src/YZWater.Avalonia/App.axaml.cs

@@ -2,7 +2,6 @@ using Avalonia;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Markup.Xaml;
 using Avalonia.Markup.Xaml.Styling;
-using Avalonia.Threading;
 using YZWater.Avalonia.Controls;
 using YZWater.Avalonia.Views;
 using YZWater.Core.Services;
@@ -18,13 +17,11 @@ public partial class App : Application
     {
         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;
     }
 
@@ -32,8 +29,6 @@ public partial class App : Application
     {
         DatabaseService.Initialize();
         PlcService.Initialize();
-
-        // 鍒濆鍖栦富棰樹负娴呰壊
         ThemeHelper.SetTheme(false);
 
         if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
@@ -44,17 +39,19 @@ public partial class App : Application
         base.OnFrameworkInitializationCompleted();
     }
 
-    /// <summary>
-    /// 搴旂敤褰撳墠涓婚
-    /// </summary>
     private void ApplyTheme()
     {
         var resources = Resources.MergedDictionaries;
-        if (resources.Count == 0 || _darkTheme == null || _lightTheme == null) return;
+        if (resources.Count == 0) return;
+
+        // 娓呯┖骞堕噸鏂板姞杞戒富棰樿祫婧
+        var targetTheme = ThemeService.Instance.IsDarkTheme ? _darkTheme : _lightTheme;
+        if (targetTheme == null) return;
 
-        resources[0] = ThemeService.Instance.IsDarkTheme ? _darkTheme : _lightTheme;
+        resources.Clear();
+        resources.Add(targetTheme);
 
-        // 鏇存柊鐢诲埛缂撳瓨骞堕氱煡鎺т欢閲嶇粯
+        // 鍚屾鏇存柊鐢诲埛缂撳瓨
         ThemeHelper.SetTheme(ThemeService.Instance.IsDarkTheme);
     }
 }