[英]UWP/WinUI3 How to get system theme in C#?
是否有编程方式来访问系统主题(即 Windows 主题)?
类似的问题#UWP get system theme (Light/Dark)
在这里得到回答:
var DefaultTheme = new Windows.UI.ViewManagement.UISettings();
var uiTheme = DefaultTheme.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background).ToString();
但正如tipa
评论的那样,接受的答案提出了一种访问应用程序主题的方法,而不是 Windows 的主题。
因此,我想知道是否有其他方法可以访问系统主题。
这是我之前在 WPF 应用程序中使用的一种方法,用于确定 Windows 是高对比度主题还是深色主题。
它已经有一段时间没有更新了,所以它可能已经过时了,但可能是一个起点? 如果需要,您可以轻松地调整它以返回一个枚举或布尔值来表示亮/暗。
private static string GetWindowsTheme()
{
string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
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";
}
}
尝试这个:
[DllImport("UXTheme.dll", SetLastError = true, EntryPoint = "#138")]
public static extern bool ShouldSystemUseDarkMode();
如果系统使用暗模式,它将返回 true。
那不是应用程序的主题。
在 WinUI 3 中,枚举ApplicationTheme
在Microsoft.UI.Xaml命名空间中定义。
public enum ApplicationTheme
{
//
// Summary:
// Use the **Light** default theme.
Light,
//
// Summary:
// Use the **Dark** default theme.
Dark
}
你可以得到这样的主题。
public App()
{
this.InitializeComponent();
ApplicationTheme theme = RequestedTheme;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.