Use a resource dictionary as theme in Silverlight

Posted by SaphuA on Stack Overflow See other posts from Stack Overflow or by SaphuA
Published on 2010-06-11T18:19:50Z Indexed on 2010/06/12 10:33 UTC
Read the original article Hit count: 430

Filed under:
|
|
|

Hello,

I have developed an application which allows the user to switch between themes. I'm doing this by including the xaml file as a resource in my project and using the following code:

MainTheme.ThemeUri = new Uri("SilverlightApplication1;component/Themes/[ThemeName]/Theme.xaml", UriKind.Relative);

This worked well, untill I found these themes: http://timheuer.com/blog/archive/2010/05/17/silverlight-4-tools-released-and-new-application-templates.aspx

The difference is that these themes consist of multiple files. So I made a Theme.xaml file that only includes MergedDictionaries so I could still use the code above. This is the Theme.xaml file for the Cosmopolitan theme.

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="CoreStyles.xaml"/>
        <ResourceDictionary Source="SDKStyles.xaml"/>
        <ResourceDictionary Source="Styles.xaml"/>
        <ResourceDictionary Source="ToolkitStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

However, when I run the c# code above I get the following exception:

System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.ResourceDictionary.Source'.

Just to be clear, using the MergedDictionaries method does work when I set it in my App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/Cosmopolitan/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

What am I doing wrong?

Thanks!

© Stack Overflow or respective owner

Related posts about silverlight-4.0

Related posts about themes