在 Silverlight 中使用资源字典作为主题
我开发了一个允许用户在主题之间切换的应用程序。我通过将 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>
我做错了什么?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用 MergedDictionary 时,您必须使用完全限定名称,如下所示。
另请注意,不应错过程序集名称之前的斜杠。换句话说,它应该像
不像
HTH
When you are using MergedDictionary you have to use fully qualified name like below.
Also, note that you should not miss the slash before the assembly name. In other words, it should be like
not like
HTH