Friday, October 26, 2012

Force Windows Phone Theme

Windows Phone applications inherits the theme of the OS on launch.

When the UI is designed specially for the dark theme it won't look well on the light theme, or vice versa.

To prevent this the application can force the default dark or light theme.

In the application class' constructor put this code to force the dark theme:
if ((Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible) MergeCustomColors("/Themes/DarkStyles.xaml");
Or this code to force the light theme:
if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible) MergeCustomColors("/Themes/LightStyles.xaml");
Anywhere in your project put this method:
private void MergeCustomColors(String Theme)
{
    ResourceDictionary Dictionaries = new ResourceDictionary();
    String source = String.Format(Theme);
    var themeStyles = new ResourceDictionary { Source = new Uri(source, UriKind.Relative) };
    Dictionaries.MergedDictionaries.Add(themeStyles);
    ResourceDictionary appResources = Current.Resources;
    foreach (DictionaryEntry entry in Dictionaries.MergedDictionaries[0])
    {
        SolidColorBrush ColorBrush = entry.Value as SolidColorBrush;
        SolidColorBrush ExistingBrush = appResources[entry.Key] as SolidColorBrush;
        if (ExistingBrush != null && ColorBrush != null) { ExistingBrush.Color = ColorBrush.Color; }
    }
}
The code assumes that the projects contains the files DarkStyles.xaml and LightStyles.xaml in a folder named Themes.

A demo project can be found here.

8 comments:

  1. Hello there!

    I'm doing a manual pingback from my blog at http://win8rants.wordpress.com/ where I linked your excellent article on the very topic I'm doing an investigation on. My problem is I can't get to work the overridden brushes on Windows Phone 8. Did you ran this on an earlier phone OS, or am I messing something up?

    Thank you!
    Zeon

    ReplyDelete
    Replies
    1. Hi there!

      I haven't tested this code in WP8 just yet.

      It runs perfectly in WP7, however, things may be different in WP8.

      Do you get any error or the colors simply doesn't change?

      Delete
    2. The colors won't change.

      If I put a breakpoint on the conditional ExistingBrush.Color = ColorBrush.Color; block, I can see it fire, but it won't change anything. When switching to the style sheet I dug up from the WinRT resources, it won't fire at all, so I guess that is a dead-end. (Maybe I'll try to find the new Phone style Xaml and swap that with yours, but this WinRT was quite easy to find and I gave it a shot.)

      Delete
    3. Finally I did dug up the WP8 style after realizing that I was on the wrong track with the WinRT resources. And it DID work after some mess with a typo :)

      Thank you for your help and great post!

      Delete
  2. I have updated my post with some additional info!

    thanks

    ReplyDelete
  3. The style files are from WP7, extracting the original style files from WP8 and merging them may be better.

    In WP7 the merge is done in the constructor of the application class, perhaps in WP8 the merge must be done at some other event.

    ReplyDelete
    Replies
    1. Is this not grounds for MS Certification failure?

      Delete
    2. See this app created by MS:

      http://www.istartedsomething.com/20100815/conceptual-usga-golf-scoring-app-microsoft-windows-phone-7-apps-done-right/

      The app is always in a light theme even if the OS theme is dark.

      Another example is the mail application of the Windows Phone 7 (by what I read, WP8 will allow the user to choose the dark theme for the mail app).

      So, IMO, forcing the theme will not fail the MS certification.

      Delete