using Microsoft.UI; using Windows.UI; using Windows.UI.ViewManagement; namespace XFEExtension.NetCore.WinUIHelper.Utilities.Helper; /// /// 应用程序主题帮助类 /// public static class AppThemeHelper { /// /// 主窗口(请勿在App构造函数内对其赋值) /// public static Window? MainWindow { get; set; } /// /// 应用程序主题 /// public static ElementTheme Theme { get; set; } = ElementTheme.Default; /// /// 更改应用程序主题 /// /// 目标主题 public static void ChangeTheme(ElementTheme theme) { if (MainWindow is null) return; if (MainWindow.Content is FrameworkElement rootElement) { rootElement.RequestedTheme = theme; } if (theme == ElementTheme.Default) { var uiSettings = new UISettings(); var background = uiSettings.GetColorValue(UIColorType.Background); theme = background == Colors.White ? ElementTheme.Light : ElementTheme.Dark; } if (theme == ElementTheme.Default) { theme = Application.Current.RequestedTheme == ApplicationTheme.Light ? ElementTheme.Light : ElementTheme.Dark; } MainWindow.AppWindow.TitleBar.ButtonForegroundColor = theme switch { ElementTheme.Dark => Colors.White, ElementTheme.Light => Colors.Black, _ => Colors.Transparent }; MainWindow.AppWindow.TitleBar.ButtonHoverForegroundColor = theme switch { ElementTheme.Dark => Colors.White, ElementTheme.Light => Colors.Black, _ => Colors.Transparent }; MainWindow.AppWindow.TitleBar.ButtonHoverBackgroundColor = theme switch { ElementTheme.Dark => Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF), ElementTheme.Light => Color.FromArgb(0x33, 0x00, 0x00, 0x00), _ => Colors.Transparent }; MainWindow.AppWindow.TitleBar.ButtonPressedBackgroundColor = theme switch { ElementTheme.Dark => Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF), ElementTheme.Light => Color.FromArgb(0x66, 0x00, 0x00, 0x00), _ => Colors.Transparent }; MainWindow.AppWindow.TitleBar.BackgroundColor = Colors.Transparent; } /// /// 转换为应用程序主题 /// /// 目标主题 /// 应用程序主题 /// public static ApplicationTheme ToAppTheme(this ElementTheme theme) => theme switch { ElementTheme.Default => Application.Current.RequestedTheme, ElementTheme.Light => ApplicationTheme.Light, ElementTheme.Dark => ApplicationTheme.Dark, _ => throw new NotImplementedException() }; }