【发布时间】:2021-03-30 09:40:16
【问题描述】:
我将为一些控件创建一个简单的主题。我现在的问题是从第一个 ResourceDictionary 访问到第二个 ResourceDictionary。
示例:ResourceDictionary1 (DicColors) 包含颜色,第二个 ResourceDictionary (DicThemeColor) 需要来自 DicColors 的颜色来构建 SolidColorBrush:
DicColors
<Color x:Key="Color_System_Green">##8ec760</Color>
<Color x:Key="Color_System_Blue">#53baf2</Color>
<Color x:Key="Color_System_Orange">#ff9a1e</Color>
DicThemeColor
<SolidColorBrush x:Key="Button.Static.Background" Color="{StaticResource Color_System_Green}"/>
<SolidColorBrush x:Key="Button.MouseOver.Background Color="{StaticResource Color_System_Blue}"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="{StaticResource Color_System_Orange}"/>
目标是,在单个 ResourceDictionary 中定义颜色,其他 ResourceDictionaries 可以从中使用这些颜色。我现在的问题是,我没有从 DicThemeColor 到 DicColors
的访问权限示例:我将更改背景。这里是来自按钮默认模板的 sn-p。 在 App.xaml 中,我合并了 ResourceDictionaries。
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/TestApp;component/Styles/Colors/DicColors.xaml" />
<ResourceDictionary Source="pack://application:,,,/TestApp;component/Styles/Controls/DicThemeColor.xaml" />
</ResourceDictionary.MergedDictionaries>
这里是按钮的默认模板。我只改变了背景。你可以在这里看到最后一行的变化。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp.Styles.Controls">
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="{DynamicResource Color.Button.Static.Background}"/>
当我将此样式用于按钮时,我得到的是白色背景而不是绿色。我不知道为什么。
希望你能帮助我。
问候
【问题讨论】: