如何在其他 ResourceDictionaries 中使用 App.xaml 中定义的 StaticResources?

How can I use StaticResources defined in App.xaml in other ResourceDictionaries?

我在 App.xaml 中定义了一些 StaticResource 样式以在整个应用程序中使用它:

<Application x:Class="TaskListApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:TaskListApp"
             StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <SolidColorBrush x:Key="primaryColor" Color="#B5B4D9"/>
        <SolidColorBrush x:Key="secondaryColor" Color="#393E59"/>
        <SolidColorBrush x:Key="backgroundColor" Color="#2a2e42"/>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="./Themes/CheckboxTheme.xaml"/>
            <ResourceDictionary Source="./Themes/TasksTheme.xaml"/>
            <ResourceDictionary Source="./Themes/TaskListTheme.xaml"/>
            <ResourceDictionary Source="./Themes/MenuButtonTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

在 MainWindow.xaml 中使用它不会出错并且工作正常:

<Grid Grid.Row="2"
      Background="{StaticResource backgroundColor}"
</Grid>

但是当我将它添加到 ResourceDictionary 中时(在本例中 TaskListTheme.xaml),我遇到了一个异常:

Exception: Cannot find resource named 'primaryColor'. Resource names are case sensitive.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="ListBoxItem" x:Key="TaskListTheme">
    <Setter Property="Margin" Value="0,0,0,5"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="50"/>
                    </Grid.RowDefinitions>
                    <Label Grid.Column="1" Content="{Binding TaskListItemName}"
                             Margin="0"
                             Height="50"
                             Background="{StaticResource primaryColor}"
                             Padding="10,0,0,0"
                             Foreground="{StaticResource secondaryColor}"
                             FontSize="15"
                             FontWeight="SemiBold"
                             HorizontalContentAlignment="Stretch"
                             VerticalContentAlignment="Center"
                             BorderThickness="0"/>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

如何解决此类问题?

如果 TaskListTheme.xaml 中的资源依赖于另一个资源字典中定义的资源,您应该将后者合并到 TaskListTheme.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Colors.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

因此将您的 *Color 笔刷资源移动到 Colors.xaml,然后将 Colors.xaml 合并到其他资源字典或合并到 App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Colors.xaml"/>
            <ResourceDictionary Source="./Themes/CheckboxTheme.xaml"/>
            <ResourceDictionary Source="./Themes/TasksTheme.xaml"/>
            <ResourceDictionary Source="./Themes/TaskListTheme.xaml"/>
            <ResourceDictionary Source="./Themes/MenuButtonTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

我觉得mm8的回答很标准。也就是说,直接将Application.Resources下的资源移动到MergedDictionaries里面的ResourceDictionary中,也可以达到同样的效果。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <SolidColorBrush x:Key="primaryColor" Color="#B5B4D9"/>
                <SolidColorBrush x:Key="secondaryColor" Color="#393E59"/>
                <SolidColorBrush x:Key="backgroundColor" Color="#2a2e42"/>
            </ResourceDictionary>
            <ResourceDictionary Source="./Themes/CheckboxTheme.xaml"/>
            <ResourceDictionary Source="./Themes/TasksTheme.xaml"/>
            <ResourceDictionary Source="./Themes/TaskListTheme.xaml"/>
            <ResourceDictionary Source="./Themes/MenuButtonTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>