将合并字典添加到合并字典 [英] Adding a Merged Dictionary to a Merged Dictionary

查看:45
本文介绍了将合并字典添加到合并字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法将合并字典添加到 XAML 中的合并字典集合中.

I can't seem to be able to add a merged dictionary to a collection of merged dictionaries within XAML.

主题.xaml

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Mine;component/Themes/Palette.Blue.xaml"/>
    <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
</ResourceDictionary.MergedDictionaries>

应用资源

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/> 
            <!--
            <ResourceDictionary Source=="/Mine;component/Themes/Palette.Blue.xaml"/>
            <ResourceDictionary Source="/Mine;component/Themes/Template.xaml"/>
            -->
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

注意:如果我将两个 ResourceDictionaries 都放在 Appication.Resources MergedDictionary 中(注释掉 theme.xaml 并取消注释其他两个词典),它们都会正确加载.但是,我们的资源定义方式,这可能意味着将加载相当多的资源,对于动态加载,我希望能够定义模板.

Note: if I put both ResourceDictionaries in the Appication.Resources MergedDictionary (comment out the theme.xaml and uncomment the other two dictionaries) they both load correctly. However, the way our resources are defined, this may mean that quite a few resources will be loaded, and for dynamic loading I'd like to be able to define templates.

推荐答案

这是一个优化错误,请参阅 这个链接

This is an optimization bug, see this link

关于每个对象的创建XAML,如果存在默认样式(即带有类型键的样式)样式应该被应用.尽你所能想象一下有几种表现优化以实现(暗示)查找重量尽可能轻.一其中之一是我们不向内看资源字典,除非它们是标记为包含默认风格".有一个错误:如果你所有的默认样式嵌套在合并中三层深的字典(或更深)顶级词典没有被标记,因此搜索会跳过它.解决方法是设置一个默认值风格的东西,任何东西,在根字典.

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as "containing default Styles". There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

所以在根字典中添加一个虚拟样式可以解决这个问题.示例

So adding a dummy style to the root dictionary fixes this. Example

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Mine;component/Themes/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />
    </ResourceDictionary>
</Application.Resources>

这篇关于将合并字典添加到合并字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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