在Silverlight中使用资源字典作为主题

Sap*_*huA 7 resources themes silverlight-4.0

我开发了一个允许用户在主题之间切换的应用程序.我这样做是通过将xaml文件作为资源包含在我的项目中并使用以下代码:

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

这种运作良好,直到我发现了以下主题:http://timheuer.com/blog/archive/2010/05/17/silverlight-4-tools-released-and-new-application-templates.aspx

不同之处在于这些主题由多个文件组成.所以我创建了一个仅包含MergedDictionaries的Theme.xaml文件,所以我仍然可以使用上面的代码.这是Cosmopolitan主题的Theme.xaml文件.

<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>
Run Code Online (Sandbox Code Playgroud)

但是,当我运行上面的c#代码时,我得到以下异常:

System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.ResourceDictionary.Source'.
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,当我在App.xaml中设置它时,使用MergedDictionaries方法确实有效:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/Cosmopolitan/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢!

Pri*_*aka 10

当您使用MergedDictionary时,您必须使用如下所示的完全限定名称.

<ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/SilverlightApplication1;component/Themes/Cosmopolitan/Theme.xaml"/>
Run Code Online (Sandbox Code Playgroud)

另请注意,在程序集名称之前不要错过斜杠.换句话说,应该是这样的

Source="/SilverlightApplication1;
Run Code Online (Sandbox Code Playgroud)

不喜欢

Source="SilverlightApplication1;
Run Code Online (Sandbox Code Playgroud)

HTH

  • 你不必把'组件'放在程序集名称"/SilverlightApplication1 ;component/Themes/Cosmopolitan/Theme.xaml"之后 (5认同)