How do I know what the theme is selected in the settings (light or dark)? I want to use a conditional statement such as
if (darkTheme) {..}
else {..}
You want to find your response in the official MSDN page for Theme on Windows Phone.
In the part "Determining Theme Background" that indicate :
// 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";
}
Also, in this page, you've a part on the "theme accent color". To recover the two main colors defined by the user ( background and accent color).
if( (Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] )
...
else
...
I find the easiest approach to determine the theme is to use:
public bool darkTheme = ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible);
is darkTheme is true then the selected theme is dark, and false for light.
Then in whatever procedure just use a simple if
statement, such as:
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;
}
}
BONUS: Install ThemeManager by Jeff Wilcox and switch between light and dark theme in your app with just one line of code!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.