using System.Windows; // 双开 WPF + WinForms 时,Application 在两个命名空间都有,固定走 WPF 的。 using Application = System.Windows.Application; namespace WindowsGotify.Services; public static class ThemeService { public const string Light = "Light"; public const string Dark = "Dark"; public const string System = "System"; public static event EventHandler? Changed; private static string _current = Light; public static string Current => _current; /// /// 切换到指定主题。currentTheme = "Light" / "Dark" / "System"(System 暂时等同 Light,V1 简化)。 /// public static void Apply(string themeName) { if (string.IsNullOrWhiteSpace(themeName)) themeName = Light; // "System" 在 V1 简化:用 Light(后续可接 Windows 主题 API) if (themeName == System) themeName = Light; _current = themeName; var merged = Application.Current.Resources.MergedDictionaries; // 移除已有的 Themes/* 字典 for (int i = merged.Count - 1; i >= 0; i--) { var src = merged[i].Source?.ToString(); if (src != null && src.Contains("/Themes/", StringComparison.OrdinalIgnoreCase)) merged.RemoveAt(i); } // 加载新主题 var newDict = new ResourceDictionary { Source = new Uri($"/Themes/{themeName}.xaml", UriKind.Relative) }; merged.Add(newDict); Changed?.Invoke(null, EventArgs.Empty); } public static void Toggle() { Apply(_current == Dark ? Light : Dark); } }