簡體   English   中英

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>

應用程序.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM