|
|
@@ -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);
|
|
|
}
|
|
|
}
|