How To Detect Theme in Windows Phone Application Using C#

In this article we will detect the theme of a Windows Phone using C#.

Introduction

In this article we will detect the theme of a Windows Phone using C#.

There are two themes available for users in their Windows Phone, "dark theme" and "light theme".

Procedure

Step 1: Create a new “Windows Phone Application” in Visual Studio and name the project as you choose (I here named it BackColorTheme). Now a new Windows Phone Application Page (MainPage.xaml) will be generated.

Step 2: Now go to the toolbox and a Button Control in your project and name it "buttonCheckTheme" and it's content "Check Theme".

Your MainPage will look like this:

MainPage

Step 3: Navigate to the MainPage.xaml.cs file, the code portion of the project, and add the following code initially:
 

public partial class MainPage : PhoneApplicationPage

{

     //Storing the Value of dark and light Color in a

     // variable 'darkthemeBack' and 'lightthemeBack'   

    // respectively.

    private Color darkthemeBack = Color.FromArgb(255,0,0,0);

    private Color lightthemeBack = Color.FromArgb(255,255,255,255); 

    // Constructor

    public MainPage()

    {

        InitializeComponent();          

    }

}

Step 4: Now navigate to the Button Event in the project and call a method as in the following:

private void buttonCheckTheme_Click(object sender, RoutedEventArgs e)

{

     //Calling a Method name 'BackColorStatus()'

    // whenever the Button is Fired.

    BackColorStatus();

}

Step 5: Now it's time to define the method that we called in our Button event as in the following:

private void BackColorStatus()
{
}

Step 6: Add the following code inside the "BackColorStatus()" method:

private void BackColorStatus()

{

    //Extracting the Application's

    // PhoneBackGrundBrush which return something of

   // type SolidColorBrush

   SolidColorBrush backBrush = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush;

   //Checking whether it is dark theme or light and

  // display a message accordingly.

   if (backBrush.Color == darkthemeBack)

   {

       MessageBox.Show("dark Theme");

   }

   else if (backBrush.Color == lightthemeBack)

   {

       MessageBox.Show("light theme");

   }

}

Now that's all. Compile and run your project and whenever you press the Button Check Theme it will display the current theme of your phone.

Note: If you want to check it with your emulator then you can check it just by changing the theme of your emulator in the settings option.

The following shows the procedure to change the theme in the emulator:

theme in Emulator

That's all for this article. I am embedding the Source File so that you can go through it.