Using Different Resources for Windows Phone 7 Themes
August 19, 2010 6 Comments
As you know, Windows Phone 7 devices support theming: they allow you to pick Dark or Light backgrounds and choose an accent color. It’s really easy to configure your application to use predefined styles like “Disabled” or “Active” – they react to theme changing and you always get a well-colored UI. The real question is how to use different images for each theme. There is no standard way of detecting a theme and distinguishing resources based on it. I’ve surfed the web and found a solution proposed by Derik Whittaker – it detects theme by checking the color of the background. But what to do with obtained theme? The variants I’ve found at MSDN forums was not nice enough so I had to write something myself. My solution is resources-based and allows you to distinguish UIs for each theme just by adding two dictionary entries with same key. So let’s start.
Here we define ApplicationTheme enum that will distinguish our themes:
Then we implement a solution by Derik Whittaker:
It’s rather simple: it takes current PhoneForegroundColor property from application resources and determines theme based on it.
Now we’re going to organize an access to theme-specific resources via the ThemeResources class:
Right after first call to ThemeResources they will initialize ResourceManager based on ApplicationTheme. Files used for resource storage should be it the same namespace with ThemeResources file:

These .resx files should be named appropriately for each valid ApplicationTheme enumeration value: Dark or Light.
That’s it! All is left is using our ThemeResources class. Let’s define a property with some text and then bind to it:
This code will automatically determine what theme is enabled and will pick appropriate application title:
You can easily store not only application titles but also for image URLs (what this solution was really intended for). You may need to show a white image in Dark theme and black image in Light theme – use this solution to achieve this cleanly and easily.
Download sources from our Deviation Alliance Codeplex project.
Enjoy!
Pingback: Using Different Resources for Windows Phone 7 Themes « Ghazanfar Ali's Blog
Pingback: Alberto Silva
getting really sick of this stupid hack appearing all over the web. did nobody check to see that
Application.Current.Host.Background tells you the same bloody thing before they decided to spawn this hack across the internet en massee.
You’d better get sick about no easy way to detect what theme is currently sick. It doesn’t matter how you workaround this missing way: a round cake can be started eating from any side – the result is the same.
Thank you very much for your workaround! Finally it solved my problem! Just a little comment about the explanation: it should be a little bit more accurate… I had to investigate the example to fins some little tricks (like DataContext in main grid) that could not be immediately clear for a newbie of wp7 development…
anyway thanks again!
You are welcome
I’ll try to be more specific in details that are not obvious for WP7 newbies