Floating FB popout byF5debug

101 How to on Windows Phone – How to #40 – Programmatically Detect Current Theme in Windows Phone

 

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.

clip_image002

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.

clip_image004

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.

Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace F5debugHowto40
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            Visibility dbgisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

            if (dbgisibility == Visibility.Visible)
            {
                textBlock2.Text = " Dark Theme";
            }
            else
            {
                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.

Output Screen:

clip_image006

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.

clip_image008

Now navigate back to the application to see the expected output as shown in the screen below.

clip_image010

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter