简体   繁体   English

DynamicResource 不会改变颜色

[英]DynamicResource wont change color

First of all I created two themes inside resources directory首先,我在资源目录中创建了两个主题
在此处输入图像描述

Then I added light theme and changed some dynamic resources to SecondaryColor which is black然后我添加了浅色主题并将一些动态资源更改为黑色的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>

Light Theme浅色主题

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

But when I open app button background is white.但是当我打开应用程序按钮时,背景是白色的。 在此处输入图像描述

What I am doing wrong?我做错了什么?

I made a test app and it works here.我做了一个测试应用程序,它在这里工作。

This is different,there are xaml.cs files这个不一样,有xaml.cs文件

在此处输入图像描述

Added a DarkTheme.xaml ( ContentPage )添加了一个 DarkTheme.xaml ( ContentPage )

在此处输入图像描述

And change it to ResourceDictionary并将其更改为ResourceDictionary 在此处输入图像描述

This is how it looks in your example code这就是它在您的示例代码中的样子

在此处输入图像描述

LightTheme.xaml 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应用程序.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>

Your example code is here https://github.com/borisoprit/MauiApp1您的示例代码在这里https://github.com/borisoprit/MauiApp1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM