应用合并字典的动态资源后,TextBlock 前景将重置为继承值
编辑:我能够在应用程序的一个非常精简的版本中重现这一点。以下是 .zip 文件的链接
http://www.mediafire.com/?cn918gi15uph1xe
I让模块将视图添加到两个不同的区域 - 顶部的状态栏区域是问题发生的地方。奇怪的是,当相同的视图类型添加到主区域时,没有问题。状态栏区域是 ItemsControl,主区域是 ContentPresenter。这是唯一的区别。
如果您有任何见解,请告诉我!谢谢。
-----原帖-----
大家好,
我在 WPF 中看到一些奇怪的行为。我正在使用 .NET 4 和 PRISM v4。我们构建应用程序的方式是皮肤资源字典存在于它们自己的程序集中。这些模块不引用此程序集 - 相反,我们有一个皮肤管理器类,它从配置文件中读取我们想要的皮肤并将适当的组件加载到合并的字典中。合并的字典在应用程序资源中设置(我们在添加之前清除 Application.Resources.MergedDictionaries)。我们的想法是,如果需要,我们可以稍后在运行时切换皮肤,并且模块直到运行时才需要了解皮肤。
然后在我们的 xaml 中,我们使用 DynamicResource 引用样式。该问题与皮肤中定义并由键引用的 TextBlock 样式有关,例如
<TextBlock Style="{DynamicResource someKey}" ... />
样式定义字体系列、字体大小和前景。字体系列和大小应用正确(我验证了这一点)。然而,前景始终是黑色的。我使用 Snoop 和 WPF Inspector 来查看前景值是“继承的”而不是来自样式。
我还有一个继承自 TextBlock 的控件,它所做的只是添加一些属性来确定文本值应该是什么(它根本不影响样式)。我能够为 Foreground 属性添加属性更改覆盖,并发现应用了样式的前景值,然后应用了继承的值。我无法让 .NET 源代码调试工作,所以我无法弄清楚为什么/在哪里第二次调用它......
这是一个来自一个人的旧帖子的链接完全相同的问题 - 他没有找到答案,而是找到了解决方法。不幸的是,该解决方法仅适用于继承的控件(我无法为 TextBlocks 设置 InheritanceBehavior)。
http://social.msdn .microsoft.com/Forums/en-US/wpf/thread/3501ed19-ab40-4064-81b5-e9b7b9d35b56
EDIT: I was able to reproduce this in a very stripped-down version of the application. Here is a link to the .zip file
http://www.mediafire.com/?cn918gi15uph1xe
I have the module add the view to two different regions - the status bar region along the top is where the issue occurs. The weird part is, when the same view type is added to the main region, there are no problems. The status bar region is an ItemsControl and the main region is a ContentPresenter. That is the only difference.
Please let me know if you have any insight! Thanks.
-----Original Post-----
Hello all,
I am seeing some weird behavior with WPF. I'm using .NET 4 and PRISM v4. The way we have our application structured is that the skin resource dictionaries exist in their own assembly. The modules do not reference this assembly - instead we have a skin manager class that reads from a config file which skin we want and loads the appropriate components into a merged dictionary. The merged dictionary is set in the Application resources (we clear out the Application.Resources.MergedDictionaries before we add it). The idea is that we can later switch skins at runtime if needed, and the modules don't need to know about the skins until runtime.
Then in our xaml we are referencing styles using DynamicResource. The problem has to do with a TextBlock style defined in the skin and referenced by key, such as
<TextBlock Style="{DynamicResource someKey}" ... />
The style defines the font family, font size, and foreground. The font family and size are applied correctly (I verified this). The foreground, however, is always black. I used Snoop and WPF Inspector to see that the foreground value is "inherited" instead of coming from the style.
I also have a control that inherits from TextBlock and all it does is add some properties that determine what the text value should be (it doesn't affect the style at all). I was able to add a property changed override for the Foreground property and found out that the style's foreground value gets applied, and then the inherited value gets applied after that. I wasn't able to get the .NET source debugging to work so I couldn't figure out why/where it was being called from the second time...
Here is a link to an old, old post from a guy with the exact same problem - he did not find the answer, but instead a workaround. Unfortunately the workaround only works on the inherited control (I can't set InheritanceBehavior for TextBlocks).
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3501ed19-ab40-4064-81b5-e9b7b9d35b56
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,对于 TextBlock,前景属性是从其父级继承的,除非您在 TextBlock 实例上显式设置它。例如,如果您将此文本块所在的窗口或用户控件更改为蓝色前景,它可以工作吗?您可以尝试做的一件事是在样式中设置
TextElement.Foreground
,而不是仅将Foreground
设置为颜色。看看是否有效。My guess is that for TextBlock the foreground property is inherited from its parent unless you explicitly set it on your TextBlock instane. For example if you change the Window or UserControl that this Textblock is in to have a Foreground of blue does it work? One thing you can try doing is in the style instead of setting just
Foreground
to a color, setTextElement.Foreground
. See if that works.它没有解释问题存在的原因,但在这里找到了修复:
http://compositewpf.codeplex。 com/discussions/257596
修复方法是在创建/添加视图到 shell 之前加载皮肤。
It doesn't explain why the problem exists, but a fix was found here:
http://compositewpf.codeplex.com/discussions/257596
The fix is to load the skins before creating/adding views to the shell.