Ame*_*nti 43 wpf colors skinning resourcedictionary
我在我的应用程序中实现了皮肤.应用程序加载其Brushes.xaml资源字典,该字典使用驻留在特定于皮肤的资源字典中的颜色.因此,根据所选皮肤,仅加载一个Color.xaml.
皮肤特异性Color.xaml
<Color x:Key="TextBoxBackgroundColor">#C4AF8D</Color>
<Color x:Key="TextBoxForegroundColor">#6B4E2C</Color>
<Color x:Key="ToolBarButtonForegroundColor">#6B4E2C</Color>
Run Code Online (Sandbox Code Playgroud)
Brushes.xaml:
<SolidColorBrush
x:Key="TextBoxBackground"
Color="{DynamicResource TextBoxBackgroundColor}" />
<SolidColorBrush
x:Key="TextBoxForeground"
Color="{DynamicResource TextBoxForegroundColor}" />
Run Code Online (Sandbox Code Playgroud)
如您所见,多种颜色(TextBoxForegroundColor和ToolBarButtonForegroundColor)是相同的.我想绕过它,因为它变得越来越混乱,特别是因为使用的颜色不能通过它们的十六进制值识别.您现在可以建议将两种颜色合并为一种,但我有一些皮肤,其中TextBoxForegroundColor与ToolBarButtonForegroundColor不同.
我想做的是这样的事情:
<Color x:Key="DarkBrown">#C4AF8D</Color>
<Color x:Key="TextBoxBackgroundColor" Color={StaticResource DarkBrown} />
<Color x:Key="ToolBarButtonForegroundColor" Color={StaticResource DarkBrown} />
Run Code Online (Sandbox Code Playgroud)
这在Xaml中是否可行?我找不到办法.
H.B*_*.B. 43
这个?
<Color x:Key="DarkBrown">#C4AF8D</Color>
<DynamicResource x:Key="TextBoxBackgroundColor" ResourceKey="DarkBrown"/>
<DynamicResource x:Key="ToolBarButtonForegroundColor" ResourceKey="DarkBrown"/>
Run Code Online (Sandbox Code Playgroud)
对于更高级的用例和多级别的别名,请参阅此答案.
你为什么不把Brushes.xaml特定于皮肤?然后你会有这个:
<Color x:Key="DarkBrown">#C4AF8D</Color>
<SolidColorBrush x:Key="TextBoxBackgroundBrush" Color={StaticResource DarkBrown} />
<SolidColorBrush x:Key="ToolBarButtonForegroundBrush" Color={StaticResource DarkBrown} />
Run Code Online (Sandbox Code Playgroud)
另一个支持制作特定于皮肤的画笔的一点是,在某些情况下,您需要ToolBarButtonForegroundBrush
在一个皮肤中制作纯色画笔,在另一个皮肤中制作渐变画笔.
HB的答案非常有趣,我已经玩了很多,因为我想完全按照这个问题要求去做.
我注意到使用DynamicResource不适用于WPF 3.5.也就是说,它在运行时抛出一个异常(Amenti谈到的那个).但是,如果您创建引用要共享的颜色的颜色... StaticResource,它适用于WPF 3.5和WPF 4.0.
也就是说,这个xaml适用于WPF 3.5和WPF 4.0:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ColorsReferencingColors.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480"
>
<Window.Resources>
<Color x:Key="DarkBlue">DarkBlue</Color>
<StaticResource x:Key="EllipseBackgroundColor" ResourceKey="DarkBlue"/>
<SolidColorBrush
x:Key="ellipseFillBrush"
Color="{DynamicResource EllipseBackgroundColor}"
/>
</Window.Resources>
<Grid>
<StackPanel Margin="25">
<Ellipse
Width="200"
Height="200"
Fill="{DynamicResource ellipseFillBrush}"
/>
</StackPanel>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
值得一提的另一件事是,这种方法对那里的设计师造成了严重破坏(即Visual Studio 2008和2010设计师,Blend 3和4设计师).我推测这就是为什么Kaxaml 1.7不喜欢HB的xaml(如果你跟随HB的回答评论流).
虽然它打破了设计师的简单测试案例,但它似乎并没有打破我在日常工作中工作的大规模应用程序的设计界面.简直怪异!换句话说,如果你关心设计师仍在使用的东西,无论如何都要尝试这种方法......你的设计师仍然可以工作!
归档时间: |
|
查看次数: |
48027 次 |
最近记录: |