开发者

Use a resource dictionary as theme in Silverlight

开发者 https://www.devze.com 2023-01-03 08:47 出处:网络
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:

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://sche开发者_JAVA百科mas.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!


When you are using MergedDictionary you have to use fully qualified name like below.

<ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/SilverlightApplication1;component/Themes/Cosmopolitan/Theme.xaml"/>

Also, note that you should not miss the slash before the assembly name. In other words, it should be like

Source="/SilverlightApplication1;

not like

Source="SilverlightApplication1;

HTH

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号