How to program the current set theme in Windows Phone 8.1?

I want to check if the user has installed a light or dark theme. Is it possible to do this programmatically in Windows Phone 8.1 (application for the store).

+6
source share
2 answers

Here on MSDN , you'll find code examples that you can use to determine your current topic — by comparing resources. For instance:

private bool IsDarkTheme() { return (double)Application.Current.Resources["PhoneDarkThemeOpacity"] > 0; } 

But - I have included some issues that execute the above line in the WP8.1 Runtime - it could not find the requested key. As it turned out, the above code will only work on WP8.1 Silverlight (also WP8.0) .

But (again) nothing stands in your way to define your own ThemeResource and check its status:

In app.xaml - define some ThemeResources:

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Light"> <x:Boolean x:Key="IsDarkTheme">false</x:Boolean> </ResourceDictionary> <ResourceDictionary x:Key="Dark"> <x:Boolean x:Key="IsDarkTheme">true</x:Boolean> </ResourceDictionary> <ResourceDictionary x:Key="Default"> <x:Boolean x:Key="IsDarkTheme">false</x:Boolean> </ResourceDictionary> </ResourceDictionary.ThemeDictionaries> </ResourceDictionary> </Application.Resources> 

Then you can use, for example, a property in your code:

 public bool IsDarkTheme { get { return (bool)Application.Current.Resources["IsDarkTheme"]; } } 

Please also note that in some cases you may need to check HighContrast - according to MSDN you can do this by setting the AccessibilitySettings class or expand your own created ThemeResource object with HighContrast values.

+7
source

To check which theme is active, you can use the RequestedTheme property of the MSDN Application object

 var isDark = Application.Current.RequestedTheme == ApplicationTheme.Dark; 
+3
source

Source: https://habr.com/ru/post/972514/


All Articles