In this tutorial we are going to see how to programmatically detect which is the current theme that is running in a device from any application. It would be useful for application that are required to change the theme based on the selection or any requirement that requires for a theme change. PhoneDarkThemeVisibility class will be used to detect the theme to which the device is registered, and theme can be changed easily from the setting page. [more]
Let us see how exactly we can detect the theme in this article, Open Visual Studio and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below.
Now drag and drop few controls which are used to display the detected theme, basically a text block which shows the end result of the selected theme just for our reference as shown in the screen below.
Now we need to write the below code in the code behind event which basically detect the theme running the background and will provide us with an option to do some changes as per the selection as shown in the code below.
public partial class MainPage : PhoneApplicationPage
Visibility dbgisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];
if (dbgisibility == Visibility.Visible)
textBlock2.Text = " Dark Theme";
textBlock2.Text = " Light Theme";
Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.
Now change the Theme by Pressing the Windows Button in the Emulator and going to Settings, then select theme to Light as shown in the screen below. Here basically we are changing the theme so that the application will detect the theme without changing any code.
Now navigate back to the application to see the expected output as shown in the screen below.
That’s it from this tutorial, we have seen how to programmatically detect the selected theme which can be used to play around with in the application for better visibility. See you all in the next tutorial until then Happy Programming.