1
votes

In short my questions are: How to switch ResourceDictionaries in a WP7.1 app? Is there an easier way than the one I am trying to do?

Details...

In a Windows Phone 7 project I need to replace the application resources according to the user's theme (light/dark). The idea in a nutshell is obvious:
    a) create a ResourceDictionary variable
    b) get the appropriate styles and brushes resource files according to the current theme
    c) add the above resources to the Application.Current.Resources.MergedDictionaries

Here's what I am doing:

1) Folder structure for resources in the "View" project:
    Resources
        Dark
            Brushes.xaml
            Styles.xaml
        Light
            Brushes.xaml
            Styles.xaml

2) The styles in the 2 Brushes.xaml files have the same Keys. Same with Styles.xaml.

3) In my first attempt (assuming light theme selected) I get an "unspecified error" on the second line.

var uriPath = "Resources/Light/Brushes.xaml"; 
var brushes = new ResourceDictionary {Source = new Uri(uriPath, UriKind.Relative)};
dic.MergedDictionaries.Add(brushes);

(FYI I tried with Resource, Page and Content build actions)

4) My second attempt gives me hope, as I successfully manage to stuff Brushes.xaml into the App's MergedDictionaries:

string xaml;
var uriPath = "Resources/Light/Brushes.xaml";
var brushesUri = new Uri(uriPath, UriKind.Relative);
var brushesStream = Application.GetResourceStream(brushesUri);
using (var brushesStreamReader = new StreamReader(brushesStream.Stream))
    xaml = brushesStreamReader.ReadToEnd();
var dic = new ResourceDictionary();
dic.MergedDictionaries.Add((ResourceDictionary)XamlReader.Load(xaml));

5) In order for brushesStreamReader to not be null in the code above I have to set the xaml files to "Content". (why?)

6) The second problem with my code in step 4 is when attempting to do the same thing with Styles.xaml. I get a "Failed to assign to property 'System.Windows.ResourceDictionary.Source'" on the last line (dic.MergedDictionaries.Add...). Perhaps this is because Styles.xaml adds Brushes.xaml to its own MergedDictionaries:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Brushes.xaml"/>
</ResourceDictionary.MergedDictionaries>

Is it?
Thanks!

2

2 Answers

0
votes

In that case you could just grab the theme brush and analyze it. Ex:

private Color lightThemeBackground = Color.FromArgb(255, 255, 255, 255); 
private Color darkThemeBackground = Color.FromArgb(255, 0, 0, 0);  


public bool IsLightTheme()
{
     SolidColorBrush backgroundBrush = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush;   
     if (backgroundBrush.Color == lightThemeBackground) { 
          // you are in the light theme
          return true;
     } 
     else { 
          // you are in the dark theme
          return false;
     }
}
-1
votes

You shouldn't have to do this. Just use the system brushes to get all the theme colors. Here's a good overview of these brushes: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769552.aspx#BKMK_BrushResources