Question

Im trying to use ThemeManager in my WP 8 to change some default styling. I have one resource file which contains my color etc customizations.

My ThemeResources.xml

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:System="clr-namespace:System;assembly=mscorlib">
    <Color x:Key="TestColor">#FF2c5f8c</Color>
    <SolidColorBrush x:Key="TestBrush" Color="{StaticResource TestColor}"/>
</ResourceDictionary>

Now in my App.xml im setting this as merged dictionary:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="Themes/ThemeResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <local:LocalizedStrings xmlns:local="clr-namespace:MyApp" x:Key="LocalizedStrings"/>
    </ResourceDictionary>
</Application.Resources>

And in my App.cs, in my app constructor I use themeManager:

public App()
{
    // Global handler for uncaught exceptions.
    UnhandledException += Application_UnhandledException;

    // Standard XAML initialization
    InitializeComponent();

    // Phone-specific initialization
    InitializePhoneApplication();

    // Language display initialization
    InitializeLanguage();

    // Get the custom theme
    var rd = App.Current.Resources.MergedDictionaries[0];

    // Set custom Theme, fallback to dark
    ThemeManager.SetCustomTheme(rd, Theme.Light);
    ...

Finally, in my MainPage.xml I use this TestBrush, defined in ThemeResources.xml like this:

<TextBlock Text="Testing" Foreground="{StaticResource TestBrush}"/>

Everything looks correct to me, but when I try to run the app I get the following exception:

$exception {System.Windows.Markup.XamlParseException: Cannot find a Resource with the Name/Key TestBrush [Line: 90 Position: 175]

In Visual Studio designer it displays the color correcty.

What could be the issue there?

Edit: And yes, ThemeResources.xml file's build action is set to "Resource". Still the same issue.

Was it helpful?

Solution

Read from the ThemeManager readme (https://github.com/jeffwilcox/wp-thememanager):

"NOTE: Do not put anything else in your modified ThemeResources.xaml that you will later need (if putting it in the MergedDictionary section) as part of the process is to remove the MergedDictionary once the theme has been set. For some reason if you don't the PhoneForegroundBrush doesn't stay set."

I've not yet tried myself, but I suppose that if you want to keep your TestBrush, then you need to define it in a separate XAML file. Otherwise, just redefine one of the standard theme brushes and use that instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top