DynamicResource 不会改变颜色

DynamicResource wont change color

首先我在资源目录下创建了两个主题

然后我添加了浅色主题并将一些动态资源更改为黑色的SecondaryColor

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Eden"
             x:Class="Eden.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary Source="Resources/Themes/LightTheme.xaml"/>

            <Style TargetType="Label">
                <Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
                <Setter Property="FontFamily" Value="OpenSansRegular" />
            </Style>

            <Style TargetType="Button">
                <Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
                <Setter Property="FontFamily" Value="OpenSansRegular" />
                <Setter Property="BackgroundColor" Value="{DynamicResource SecondaryColor}" />
                <Setter Property="Padding" Value="14,10" />
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>

浅色主题

<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    x:Class="Eden.Resources.Themes.LightTheme">
    <Color x:Key="PageBackgroundColor">White</Color>
    <Color x:Key="NavigationBarColor">WhiteSmoke</Color>
    <Color x:Key="PrimaryColor">WhiteSmoke</Color>
    <Color x:Key="SecondaryColor">Black</Color>
    <Color x:Key="PrimaryTextColor">Black</Color>
    <Color x:Key="SecondaryTextColor">White</Color>
    <Color x:Key="TertiaryTextColor">Gray</Color>
    <Color x:Key="TransparentColor">Transparent</Color>
</ResourceDictionary>

但是当我打开应用程序按钮时,背景是白色的。

我做错了什么?

我做了一个测试应用程序,它在这里工作。

这个不一样,有xaml.cs个文件

添加了 DarkTheme.xaml ( ContentPage )

并将其更改为 ResourceDictionary

这是您的示例代码中的样子

LightTheme.xaml

<ResourceDictionary  xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MauiApp1.LightTheme">
<Color x:Key="PageBackgroundColor">White</Color>
<Color x:Key="NavigationBarColor">WhiteSmoke</Color>
<Color x:Key="PrimaryColor">WhiteSmoke</Color>
<Color x:Key="SecondaryColor">Black</Color>
<Color x:Key="PrimaryTextColor">Black</Color>
<Color x:Key="SecondaryTextColor">White</Color>
<Color x:Key="TertiaryTextColor">Gray</Color>
<Color x:Key="TransparentColor">Transparent</Color>

App.xaml.cs

 <Application.Resources>
    <ResourceDictionary>

 <!--If you want to use LightTheme and DarkTheme add both--> 
            <ResourceDictionary Source="Resources/Themes/LightTheme.xaml"/>
            <ResourceDictionary Source="Resources/Themes/DarkTheme.xaml"/>
            <Style TargetType="Label">
                <Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
                <Setter Property="FontFamily" Value="OpenSansRegular" />
            </Style>

        <Style TargetType="Button">
            <Setter Property="TextColor" Value="{DynamicResource SecondaryColor}" />
            <Setter Property="FontFamily" Value="OpenSansRegular" />
            <Setter Property="BackgroundColor" Value="{DynamicResource SecondaryColor}" />
            <Setter Property="Padding" Value="14,10" />
        </Style>



    </ResourceDictionary>
</Application.Resources>

您的示例代码在这里https://github.com/borisoprit/MauiApp1