使用多个 MergedDictionaries 时,为什么样式在 wpf 中在运行时不起作用? [英] Why does styles don't work at runtime in wpf, when using multiple MergedDictionaries?

查看:27
本文介绍了使用多个 MergedDictionaries 时,为什么样式在 wpf 中在运行时不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我多次使用 MergedDictionaries 定义样式,它在运行时不起作用,但在 VS2010 的 WPF Designer 中它起作用.如果在运行时使用代码加载 MergedDictionaries,它也可以工作.

If i use MergedDictionaries multiple times to define styles, it doesn't work at runtime, but in WPF Designer of VS2010 it works. It also work if load MergedDictionaries using code at runtime.

为什么会这样?是我的问题还是我的问题?以及如何解决?

Why this is happening ? Is it my problem only or ? And how to solve it ?

I am using WPF4 and loading themes/styles from an assembly at application level.

不工作

<!--Application.xaml-->
 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>


<!--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="Theme/Shared.xaml" />
    <ResourceDictionary Source="Theme/Button.xaml" />
</ResourceDictionary.MergedDictionaries>

工作

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Shared.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Button.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

推荐答案

查看 这个问题.我感觉你遇到了同样的问题

See the answer to this question. I have a feeling you're having the same problem

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

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="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />
    </ResourceDictionary>
</Application.Resources>

这篇关于使用多个 MergedDictionaries 时,为什么样式在 wpf 中在运行时不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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