wpf - 使用多个 MergedDictionaries 时,为什么样式在 wpf 中在运行时不起作用?

标签 wpf xaml

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

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

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>

最佳答案

查看 this question 的答案.我感觉你遇到了同样的问题

这是一个优化错误,参见 this link

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.



所以在根字典中添加一个虚拟样式可以解决这个问题。例子
<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>

关于wpf - 使用多个 MergedDictionaries 时,为什么样式在 wpf 中在运行时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5057213/

相关文章:

WPF MouseLeftButtonUp 未触发

wpf - 将元素绑定(bind)到折叠的 UserControl 是一种草率、廉价的技巧吗?

wpf - DataGrid 未绑定(bind)(XAML + ViewModel + ObservableCollection)

wpf - 如何在控件渲染时显示一些动画?

WPF tabitem 定位

WPF pack :/[assemblyName];component/. .. vs pack ://application:, ,,/[assemblyName];component/...?

c# - 从 Web 浏览器控件捕获所有请求和响应?

c# - 如何使用 WPF 绑定(bind)触发 DataGrid 中特定单元格的更新

WPF 列表框错误模板

c# - 如何将 CollectionViewSource 传递给转换器?