蒙皮:使用一种颜色作为另一种颜色的静态资源
我在我的应用程序中实现了蒙皮。应用程序加载其 Brushes.xaml 资源字典,该字典使用驻留在特定于皮肤的资源字典中的颜色。因此,根据所选的皮肤,仅加载一种 Color.xaml。
Skin-Specific Color.xaml
<Color x:Key="TextBoxBackgroundColor">#C4AF8D</Color>
<Color x:Key="TextBoxForegroundColor">#6B4E2C</Color>
<Color x:Key="ToolBarButtonForegroundColor">#6B4E2C</Color>
Brushes.xaml:
<SolidColorBrush
x:Key="TextBoxBackground"
Color="{DynamicResource TextBoxBackgroundColor}" />
<SolidColorBrush
x:Key="TextBoxForeground"
Color="{DynamicResource TextBoxForegroundColor}" />
如您所见,多种颜色(TextBoxForegroundColor 和 ToolBarButtonForegroundColor)是相同的。我想避免这种情况,因为它变得越来越令人困惑,特别是因为所使用的颜色无法通过其十六进制值来识别。您现在可以建议将两种颜色合并为一种颜色,但我的皮肤中 TextBoxForegroundColor 与 ToolBarButtonForegroundColor 不同。
我想做的是这样的:
<Color x:Key="DarkBrown">#C4AF8D</Color>
<Color x:Key="TextBoxBackgroundColor" Color={StaticResource DarkBrown} />
<Color x:Key="ToolBarButtonForegroundColor" Color={StaticResource DarkBrown} />
这在 Xaml 中可能吗?我没有找到办法。
I implemented skinning in my application. The application loads its Brushes.xaml resource dictionary which uses colors which reside in a skin-specific resource dictionary. So only one Color.xaml is loaded depending on the chosen skin.
Skin-Specific Color.xaml
<Color x:Key="TextBoxBackgroundColor">#C4AF8D</Color>
<Color x:Key="TextBoxForegroundColor">#6B4E2C</Color>
<Color x:Key="ToolBarButtonForegroundColor">#6B4E2C</Color>
Brushes.xaml:
<SolidColorBrush
x:Key="TextBoxBackground"
Color="{DynamicResource TextBoxBackgroundColor}" />
<SolidColorBrush
x:Key="TextBoxForeground"
Color="{DynamicResource TextBoxForegroundColor}" />
As you can see, multiple colors (TextBoxForegroundColor and ToolBarButtonForegroundColor) are the same. I'd like to circumvent that as it is getting more and more confusing especially since the used colors are not recognizable by their hex value. You could advise now to merge both Colors into one but I have skins where the TextBoxForegroundColor is different from the ToolBarButtonForegroundColor.
What I would like to do is something like this:
<Color x:Key="DarkBrown">#C4AF8D</Color>
<Color x:Key="TextBoxBackgroundColor" Color={StaticResource DarkBrown} />
<Color x:Key="ToolBarButtonForegroundColor" Color={StaticResource DarkBrown} />
Is this at all possible in Xaml? I didn't find a way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这?
有关更高级的用例和多个级别的别名,请参阅此答案。
This?
For more advanced use cases and multiple levels of aliasing see this answer.
为什么不让 Brushes.xaml 专门针对皮肤?然后你会得到这样的结果:
支持使画笔针对特定皮肤的另一点是,在某些情况下,您希望使
ToolBarButtonForegroundBrush
在一种皮肤中成为纯色画笔,在另一种皮肤中使渐变画笔。Why don't you just make Brushes.xaml skin-specific? Then you will have this:
Another point in favor of making brushes skin-specific is that there are situations where you would want to make the
ToolBarButtonForegroundBrush
a solid color brush in one skin and a gradient brush in another skin.HB 的答案非常有趣,我已经尝试过很多次了,因为我想做的正是这个问题所要求的。
我注意到,使用 DynamicResource 不适用于 WPF 3.5。也就是说,它在运行时抛出异常(Amenti 谈到的异常)。但是,如果您创建引用要共享的颜色的颜色...StaticResource,则它适用于 WPF 3.5 和 WPF 4.0。
也就是说,此 xaml 适用于 WPF 3.5 和 WPF 4.0:
另一件事(再次)值得一提的是,这种方法对设计人员(即 Visual Studio 2008 和 2010 设计人员、Blend 3 和 4 设计人员)造成了严重破坏。我推测这与 Kaxaml 1.7 不喜欢 HB 的 xaml 的原因相同(如果您关注 HB 答案的评论流)。
但是,虽然它破坏了设计人员的简单测试用例,但它似乎并没有破坏我在日常工作中处理的大型应用程序的设计表面。简直奇怪!换句话说,如果您关心设计器中仍然有效的事情,请无论如何尝试此方法......您的设计器可能仍然有效!
H.B.'s answer is very interesting and I have played around with it quite a bit since I want to do exactly what this question is asking to do.
What I've noticed is that using a DynamicResource doesn't work for WPF 3.5. That is, it throws an exception at run time (the one Amenti talks about). However, if you make colors that are referencing the color you want to share ... a StaticResource, it works on both WPF 3.5 and WPF 4.0.
That is, this xaml works for both WPF 3.5 and WPF 4.0:
Another thing that bears mentioning (again) is that this approach wreaks havoc with the designers out there (i.e the Visual Studio 2008 and 2010 designers, the Blend 3 and 4 designers). I speculate that this is the same reason why Kaxaml 1.7 wasn't liking H.B.'s xaml (if you're following the comment stream on H.B.'s answer).
But while it breaks the designers for a simple test case, it doesn't seem to break the design surface for the large scale application I work on in my day job. Plain weird! In other words, if you care about things still working in the designer, try this method out anyway ... your designer may still work!
最后一部分是不可能的,因为 Color 没有 Color 属性。
颜色确实具有 R、G、B 和 A 属性。因此,您可以创建四个字节作为资源:
仍然不是您喜欢的,但它应该可以工作。
That last part is not possible as a Color has no Color property.
A Color does have an R, G, B and A property. So you could create four bytes as resources:
Still not what you might prefer but it should work.