gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:40:49 25 4
gpt4 key购买 nike

如果我多次使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com