【问题标题】:ResourceDictionary doesn´t find another ResourceDictionaryResourceDictionary 找不到另一个 ResourceDictionary
【发布时间】: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}"/>

当我将此样式用于按钮时,我得到的是白色背景而不是绿色。我不知道为什么。

希望你能帮助我。

问候

【问题讨论】:

    标签: xaml resourcedictionary


    【解决方案1】:

    我认为你需要将两个资源字典的引用更改为阅读:

    <ResourceDictionary Source="pack://application:,,,/Styles/Colors/DicColors.xaml" />
    <ResourceDictionary Source="pack://application:,,,/Styles/Controls/DicThemeColor.xaml" /> 
    

    不知道你的文件夹结构,你可能需要稍微调整一下。

    【讨论】:

    • 嗨。解决方案/样式/颜色和解决方案/样式/控件/
    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多