The Imaginative Universal

Studies in Virtual Phenomenology -- @jamesashley

Getting the current theme in WP7 for Silverlight

August 22
by James Ashley 22. August 2010 15:16

Customizing App.xaml.cs

Windows Phone devices allow users two theme options, either dark or light.

In the WP7 beta dev tools, however, there no straight-forward way for Windows Phone Silverlight developers to find out what the current theme is, though there is a hack.  The developer must interrogate the app resources to find the color of the PhoneBackGroundColor.  Under the dark theme it is black, while under the light theme it is white.

The following code can be thrown into App.xaml.cs to add a little elegance to this hack. 

First, a brief enum is needed.  It can be added in App.xaml.cs above the App class, or simply placed in its own cs file in the project:

public enum Theme{Light,Dark}

A CurrentTheme property is added to the App class:

private static Theme _currentTheme;

public static Theme CurrentTheme
{
    get { return _currentTheme; }
    private set { _currentTheme = value; }
}

And then we assign a value to CurrentTheme in the App class constructor by interrogating Resources:

public App()
{
    var bgc = Resources["PhoneBackgroundColor"].ToString();
    if (bgc == "#FF000000")
        CurrentTheme = Theme.Dark;
    else
        CurrentTheme = Theme.Light;

    //etc.

The CurrentTheme is now retrievable from anywhere in the application like so:

    switch(App.CurrentTheme)
    {
        case Theme.Dark:
            MessageBox.Show("Dark");
            break;
        case Theme.Light:
            MessageBox.Show("Light");
            break;
    }

Moreover, if a user presses the Windows button at lower center on a device and resets the theme, the new theme will be assigned to CurrentTheme when the application returns from tombstoning.

There is a rumor that in the RTM of the WP7 dev tools, two new properties will be available for picking up the current theme: DarkThemeVisibility and LightThemeVisibility.

For ease of migration to the RTM, you can add the following two properties to App.xaml.cs for now as placeholders – you should be able to simply switch them out later should the new methods become available:

public static bool DarkThemeVisibility
{
    get { return CurrentTheme == Theme.Dark; }
}

public static bool LightThemeVisibility
{
    get { return CurrentTheme == Theme.Light; }
}

Tags: ,

Silverlight | Windows Phone | Customizing App.xaml.cs

Comments

10/27/2010 6:52:26 AM #

trackback

Дайджест технических материалов #5 (Windows Phone 7)

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

Oleksandr Krakovetskiy blog

1/26/2011 10:57:58 PM #

Sunfood

Actually I am using WP7 but it is not enabling me two switch the other themes.

Any solution>?

Best Regards

Sunfood United States

3/10/2011 2:51:04 PM #

bottomless.net

Thanks

The line

var bgc = Resources["PhoneBackgroundColor"].ToString();

should be replaced by

var bg = Application.Current.Resources["PhoneBackgroundColor"].ToString();

Cheers,
http://pastaGapp.com

bottomless.net United States

6/8/2011 5:20:52 AM #

Christian Louboutin Shoes

Actually I am using WP7 but it is not enabling me two switch the other themes

Christian Louboutin Shoes United States

6/21/2011 12:31:10 PM #

pingback

Pingback from chaokuaidi.com

超快递 beta版 » Windows Phone 7 资源汇总(超全)

chaokuaidi.com

6/23/2011 5:44:16 AM #

Pandora UK

Hello, nice day.. Your article is quite uplifting. I never believed that it was feasible to accomplish something like that until after I looked over your post.

Pandora UK United States

6/23/2011 5:46:27 AM #

Pandora Bracelets

This is a fantastic guide you have more to publish these kinds of content articles since so dumb, I observed the response to my question. I wish you continued results in creating.

Pandora Bracelets United States

6/23/2011 5:47:18 AM #

Pandora Beads

Nice photos. Reminds me when i was in college. Youthful urban setting getting about in the city. Bicycle is the way.

Pandora Beads United States

6/23/2011 5:48:01 AM #

Monster Beats

nice post really informative and useful love your site looking forward for more

Monster Beats United States

6/23/2011 5:48:43 AM #

Beats Headphones

thats really a nice and informative post ,you are doing an excellent job with your site let it keep coming.

Beats Headphones United States

6/25/2011 7:52:20 AM #

louis vuitton replica hand bag



Welcome to our website. <a href="http://www.lovelybag2u.com">louis vuitton handbags wholesale</a> are one of the best selling brand sunglasses around the world. <a href="http://www.lovelybag2u.com">louis vuitton handbags wholesale</a> constantly strives to support those qualities by reinventing the concepts of comfort fit and style.

louis vuitton replica hand bag United States

7/13/2011 2:13:58 AM #

coach factory outlet

American vogue leather products <a href="...ry-outlet-mall.com"><strong>coach factory outlet</strong></a>,let you get the cheapest is the <a href="...ry-outlet-mall.com"><strong>coach outlet coupon
</strong></a>.The effect of the <a href="...eadphones-sale.net"><strong>beats headphones</strong></a> is to enjoy out,Is the effect of <a href="...dphones-sale.net"><strong>monster beats proved </strong></a>its influence. <a href="...dphones-sale.net"><strong>monster headphones</strong></a> to its strange sound conquered the human.

coach factory outlet People's Republic of China

Comments are closed

BlogRoll

Download OPML file OPML