如何在 c# 中更改 ResourceDictionary 上的 setter 属性

问题描述

我有 <ContentPage.Resources> 和几个 setter,我想在检查天气状况时将 MainPage.xaml.cs 中的 c# 代码中的字体颜色从白色更改为黑色,xaml 页面中的代码如下所示:

<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="labelStyle"
               targettype="Label">
            <Setter Property="FontSize" 
                    Value="Medium" />
            <Setter Property="Margin" 
                    Value="0,0" />
            <Setter Property="FontAttributes" 
                    Value="Bold" />
            <Setter Property="TextColor" 
                    Value="White"/>
        </Style>

        <local:LongToDateTimeConverter x:Key="longToDateTimeConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

如何在c#中获取<Setter Property="TextColor" Value="White"/>以及如何将其颜色更改为黑色?

解决方法

没有足够的信息,但我假设您可以使用 data triggers 来实现您的最终目标,即根据某些数据(属性)动态更改 TextColor

<Style x:Key="labelStyle" TargetType="Label">

            <Setter Property="FontSize" 
                    Value="Medium" />
            <Setter Property="Margin" 
                    Value="0,0" />
            <Setter Property="FontAttributes" 
                    Value="Bold" />
            <Setter Property="TextColor" 
                    Value="White"/>

    <Style .Triggers>
        <DataTrigger TargetType="Label"
                     Binding="{Binding Source=...,Path=...}"
                     Value="0">
            <Setter Property="TextColor" Value="Black" />
        </DataTrigger>
    </Style.Triggers>
</Style>
,

如果你想改变 MainPage.xaml.cs 的颜色,你可以使用稍微改变的@Cfun 的建议,而不是在 XAML 中硬编码值,你可以将绑定对象添加到你的 Setter.Value 中,例如这个:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="StyleColorChange.MainPage"
             x:Name="MyPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Style x:Key="labelStyle"
               TargetType="Label">
                <Setter Property="FontSize" 
                    Value="Medium" />
                <Setter Property="Margin" 
                    Value="0,0" />
                <Setter Property="FontAttributes" 
                    Value="Bold" />
                <Setter Property="TextColor" Value="White" />
                <Style.Triggers>
                    <DataTrigger TargetType="Label"
                                 Binding="{Binding isWeatherConditionChecked}"
                                 Value="True">
                        <Setter Property="TextColor" Value="{Binding labelStyleTextColor}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>
    
    <!--This part is only to create a sample app-->
    <StackLayout BackgroundColor="Gray">
        <Button Text="Go!" Clicked="Button_Clicked" />
        <Label x:Name="MyLabel" Text="This is a label" Style="{StaticResource labelStyle}"/>
    </StackLayout>

</ContentPage>

而在 MainPage.xaml.cs 中,您可以拥有以下内容:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace StyleColorChange
{
    public partial class MainPage : ContentPage
    {
        public string labelStyleTextColor { get; set; } = "White";
        private bool _isWeatherConditionChecked;
        public bool isWeatherConditionChecked
        {
            get { return _isWeatherConditionChecked; }
            set { _isWeatherConditionChecked = value; OnPropertyChanged("isWeatherConditionChecked"); }
        }
        public MainPage()
        {
            InitializeComponent();
            BindingContext = this;
        }
        private void Button_Clicked(object sender,EventArgs e)
        {            
            labelStyleTextColor = "Black";
            isWeatherConditionChecked = !isWeatherConditionChecked;
        }
    }
}

我添加了 isWeatherConditionCheckedlabelStyleTextColor 只是为了举例,但我想您的代码中有类似的东西可以使用。