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

发布于 2024-09-05 03:42:55 字数 1763 浏览 1 评论 0原文

我开发了一个允许用户在主题之间切换的应用程序。我通过将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2024-09-12 03:42:58

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

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

另请注意,不应错过程序集名称之前的斜杠。换句话说,它应该像

Source="/SilverlightApplication1;

不像

Source="SilverlightApplication1;

HTH

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文