Programmatically supporting Windows Phone themes with differing colors for text

The marketplace guys just rejected my application because the text was unreadable when the user switched from the “Dark theme” to the “Light theme”. Firstly, amazing that they went through manual testing to find a bug like this. Secondly, I feel silly for not finding it myself :)

My problem was that I forgot to go back to some “I’ll fix this in a minute” code, and ended up with an explicit color defined in one of my value converters. What I needed to do is to use the phone’s default brush (or to detect the theme and alter my color appropriately). Doing this from XAML is easy because you just use a built-in style; but I was assigning brushes in code, hence the problem.

This value converter needs to support text in one of two states: “Unread” and “Read”; one being dimmer than the other. So here’s the code that uses the phone’s built-in styles, which automatically honors the theme:

 _ReadBrush = App.Current.Resources["PhoneInactiveBrush"] as Brush;
_UnreadBrush = App.Current.Resources["PhoneForegroundBrush"] as Brush;

Alternatively, if you need to control things more specifically, you can detect the theme:

 var darkThemeVis = (Visibility) App.Current.Resources["PhoneDarkThemeVisibility"];

(Note, though, that in the future more themes might be added!)

Using that, you can go as far as to create different style dictionaries for different themes, and merge them with the main one depending on what you detect.

Here’s a good page that explains the available theme-compliant styles available to you: