【问题标题】:UWP/WinUI3 How to get system theme in C#?UWP/WinUI3 如何在C#中获取系统主题?
【发布时间】:2023-01-18 17:59:39
【问题描述】:
是否有访问系统主题(即 Windows 主题)的编程方法?
类似的问题#UWP get system theme (Light/Dark)得到了here的回答:
var DefaultTheme = new Windows.UI.ViewManagement.UISettings();
var uiTheme = DefaultTheme.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background).ToString();
但是作为tipa cmets,接受的答案提出了一种访问应用程序主题的方法,而不是 Windows 主题。
因此,我想知道是否有其他方法可以访问系统主题。
【问题讨论】:
标签:
c#
uwp
themes
winui-3
windows-app-sdk
【解决方案1】:
这是我之前用来确定 Windows 是高对比度主题还是深色主题的方法。
它已经有一段时间没有更新了,所以它可能已经过时了,但可能是一个起点?
private static string GetWindowsTheme()
{
string RegistryKeyPath = @"SoftwareMicrosoftWindowsCurrentVersionThemesPersonalize";
string RegistryValueName = "AppsUseLightTheme";
if (SystemParameters.HighContrast)
return "High Contrast";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
{
object registryValueObject = key?.GetValue(RegistryValueName);
if (registryValueObject == null)
return "Default";
int registryValue = (int)registryValueObject;
return registryValue > 0 ? "Default" : "Dark Theme";
}
}
【解决方案2】:
尝试这个:
[DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")]
public static extern bool ShouldSystemUseDarkMode();
如果系统使用暗模式,它将返回 true。
那不是应用程序的主题。