我如何知道在设置中选择了什么主题(浅色或深色)?我想使用条件语句,例如
if (darkTheme) {..}
else {..}
我如何知道在设置中选择了什么主题(浅色或深色)?我想使用条件语句,例如
if (darkTheme) {..}
else {..}
您想在 Windows Phone 上的主题的官方 MSDN 页面中找到您的响应。
在“确定主题背景”部分中表明:
// Determine the visibility of the dark background.
Visibility darkBackgroundVisibility =
(Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
// Write the theme background value.
if (darkBackgroundVisibility == Visibility.Visible)
{
textBlock1.Text = "background = dark";
}
else
{
textBlock1.Text = "background = light";
}
此外,在此页面中,您还参与了“主题强调色”。恢复用户定义的两种主要颜色(背景色和强调色)。
if( (Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] )
...
else
...
我发现确定主题的最简单方法是使用:
public bool darkTheme = ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible);
是 darkTheme 为 true 则所选主题为暗色,而 false 为亮色。
然后在任何程序中只使用一个简单的if
语句,例如:
if (darkTheme == true)
{
//Do some stuff related to dark theme
}
else
{
//Do some stuff related to light theme
}
// Detecting the current theme.
private static Color lightThemeBackground = Color.FromArgb(255, 255, 255, 255);
private static Color darkThemeBackground = Color.FromArgb(255, 0, 0, 0);
rivate static SolidColorBrush backgroundBrush;
internal static AppTheme CurrentTheme
{
get
{
if ( backgroundBrush == null )
backgroundBrush = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush;
if (backgroundBrush.Color == lightThemeBackground)
return AppTheme.Light;
else if (backgroundBrush.Color == darkThemeBackground)
return AppTheme.Dark;
return AppTheme.Dark;
}
}
奖励:安装 Jeff Wilcox 的 ThemeManager,只需一行代码即可在您的应用程序中切换明暗主题!