首页 > 解决方案 > ResourceDictionary 找不到另一个 ResourceDictionary

问题描述

我将为一些控件创建一个简单的主题。我现在的问题是从第一个 ResourceDictionary 访问到第二个 ResourceDictionary。

示例: ResourceDictionary1 ( DicColors ) 包含颜色,第二个 ResourceDictionary ( DicThemeColor ) 需要来自DicColors的颜色来构建 SolidColorBrush:

迪克色

<Color x:Key="Color_System_Green">##8ec760</Color>
<Color x:Key="Color_System_Blue">#53baf2</Color>   
<Color x:Key="Color_System_Orange">#ff9a1e</Color>    

Dic主题颜色

<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

示例:我将更改背景。这里是按钮默认模板的片段。在 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}"/>

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

希望您能够帮助我。

问候

标签: xamlresourcedictionary

解决方案


我认为您需要更改对两个资源字典的引用以阅读:

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

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


推荐阅读