合并字典中共享的静态资源 [英] Static resource shared in merged dictionaries

查看:125
本文介绍了合并字典中共享的静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用可以动态应用于我的应用程序的样式和模板的字典。在这个新想要的动态行为之前,我有几个资源字典,每个风格的控件一个,我合并到App.xaml:

I'm currently working on having dictionaries of styles and templates that I can dynamically apply to my application. Before this "new wanted" dynamical behavior, I had several resource dictionaries, one for each styled control, that I merged in the App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ColorsDictionary.xaml"/>
            <ResourceDictionary Source="ControlsTemplatesDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

现在,我希望我的应用程序被样式,所以我决定合并我以前的所有资源进入一个新的名为MyFirstTemplates,并只添加这个字典到App.xaml。

Now, I'd like my application to be styled, so I decided to merge all my previous resources into a new one called "MyFirstTemplates" and to add only this dictionary to the App.xaml.

新字典MyFirstTemplates.xaml:

New dictionary "MyFirstTemplates.xaml":

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">"
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ColorsDictionary.xaml"/>
        <ResourceDictionary Source="ControlsTemplatesDictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

新的App.xaml:

New App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyFirstTemplates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <Style TargetType="{x:Type Window}"/>
    </ResourceDictionary>
</Application.Resources>

注意:窗口的默认样式是更正错误WPF 4,请参阅将合并字典添加到合并字典

Note: The default style for the Window is to correct a bug of WPF 4, see Adding a Merged Dictionary to a Merged Dictionary

现在我做了这个更改,我不能使用ColorsDictionary.xaml中的颜色资源作为静态资源 ControlsTemplateDictionary.xaml了。如果我改回合并这些文件在app.xaml,一切正常。为了使其正常工作,我必须为 DynamicResource 更改这些 StaticResource 。你有什么想法为什么这不工作?

Now that I have made this change, I cannot use a color resource from "ColorsDictionary.xaml" as a StaticResource in "ControlsTemplateDictionary.xaml" anymore. If I change back to merging these files in the app.xaml, everything works. To make it work, I have to change these StaticResource for DynamicResource. Do you have any idea why this doesn't work anymore?

谢谢: - )

推荐答案

通过从App.xaml移动字典,在加载MyFirstTemplates.xaml期间,每个字典的资源不在其他资源树中。您的原始设置首先加载了ColorsDictionary,然后在加载时通​​过App资源将其添加到ControlsTemplatesDictionary。在您的新设置中,为了在App资源中可以使用颜色资源,它需要通过MyFirstTemplates进行加载,而MyFirstTemplates需要加载两个字典,这两个字典又要求访问颜色资源...所以它是一种无限循环的引用,无法静态解析。 DynamicResource可以等到所有的东西都加载,然后访问颜色,而不会出现问题。

By moving the dictionaries out of App.xaml the resources from each dictionary aren't in the other's resource tree during loading of MyFirstTemplates.xaml. Your original setup first loaded ColorsDictionary which was then available through App resources to ControlsTemplatesDictionary while it loaded. In your new setup, in order for the color resource to be available in App resources it needs to be loaded through MyFirstTemplates, which in turn requires loading of both dictionaries, which in turn requires access to the color resource... so it's sort of an infinite loop of references that can't be resolved statically. DynamicResource can wait until everything is loaded and then access the color without issue.

要修复使用Dynamic或将ColorsDictionary直接合并到ControlsTemplatesDictionary中。

To fix either use Dynamic or merge ColorsDictionary directly into ControlsTemplatesDictionary.

这篇关于合并字典中共享的静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆