【发布时间】: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