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

发布于 2025-01-19 01:17:01 字数 3008 浏览 5 评论 0原文

我在 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)时,我得到了一个例外:

异常:找不到名为“primaryColor”的资源。资源名称大小写 敏感。

<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>

如何解决此类问题?

I defined some StaticResource styles inside App.xaml to use it across the app:

<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>

Using it in MainWindow.xaml is making no errors and works just fine:

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

But when I added it inside a ResourceDictionary (in this case TaskListTheme.xaml), I got an exception:

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>

How can I resolve this kind of problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皓月长歌 2025-01-26 01:17:01

如果 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>

If a resource in TaskListTheme.xaml has a dependency on a resource that is defined in another resource dictionary, you should merge the latter into 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>

So move your *Color brush resources to Colors.xaml and then either merge Colors.xaml into the other resource dictionaries or into 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>
你在看孤独的风景 2025-01-26 01:17:01

我觉得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>

I think mm8's answer is standard way. Said that, you can just move resources directly under Application.Resources to ResourceDictionary inside MergedDictionaries to achieve the same effect.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文