【问题标题】:Use a resource dictionary as theme in Silverlight在 Silverlight 中使用资源字典作为主题
【发布时间】:2010-06-11 18:19:50
【问题描述】:

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

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

这很好,直到我找到了这些主题: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>

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

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

需要明确的是,当我在 App.xaml 中设置 MergedDictionaries 方法时,它确实有效:

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

我做错了什么?

谢谢!

【问题讨论】:

  • 在我的 Theme.xaml 文件中设置绝对路径也不起作用:(
  • 我遇到了类似的问题,原因是在引用文件时使用了反斜杠 () 而不是正斜杠 (/)。 VS 中的 xaml 解析器能够解析该位置,但在运行时会生成错误。希望这会帮助其他人。

标签: silverlight-4.0 themes resources


【解决方案1】:

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

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

另外,请注意不要错过程序集名称前的斜线。换句话说,应该是这样的

Source="/SilverlightApplication1;

不喜欢

Source="SilverlightApplication1;

HTH

【讨论】:

  • 您不必在程序集名称“/SilverlightApplication1;component/Themes/Cosmopolitan/Theme.xaml”之后放置“组件”
  • @Prince:组件部分是必不可少的。请考虑更新您的答案。更新后这将是一个很好的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多