UWP/WinUI3中如何用C#获取Windows系统主题?
如何编程获取Windows系统主题(区别于应用主题)?
你提到的UISettings.GetColorValue方法确实只能获取当前应用的主题配置,无法拿到系统全局的主题设置。以下是不同平台下获取系统主题的可行方案:
UWP 平台
可以使用Windows.System.UserProfile.UserProfilePersonalizationSettings类直接获取系统主题,这个方法返回的是系统全局的主题设置,不受应用自身主题配置影响:
using Windows.System.UserProfile; if (UserProfilePersonalizationSettings.IsSupported()) { var personalizationSettings = UserProfilePersonalizationSettings.Current; // 判断系统是否为深色主题 bool isSystemDarkTheme = personalizationSettings.SystemTheme == Windows.UI.Xaml.ElementTheme.Dark; }
Win32/.NET 平台(WPF、WinForms 等)
系统主题的配置存储在注册表中,可以直接读取对应项的值:
using Microsoft.Win32; public static bool IsSystemDarkThemeEnabled() { using var regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); if (regKey != null) { var themeValue = regKey.GetValue("SystemUsesLightTheme"); if (themeValue is int value) { // 0 = 深色主题,1 = 浅色主题 return value == 0; } } // 读取失败时返回默认值(这里默认浅色) return false; }
补充说明
- UWP的方法无需额外权限,只要是标准UWP应用即可调用;
- 注册表方法适用于大多数桌面应用,读取的是系统主题的直接配置值,修改系统主题时该值会实时更新。
内容的提问来源于stack exchange,提问作者Frank
相关产品推荐
相关产品推荐

