C# 如何在c中更改ResourceDictionary上的setter属性#
我有C# 如何在c中更改ResourceDictionary上的setter属性#,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我有和一些setter,当我检查天气状况时,我想在MainPage.xaml.cs中的c代码中将字体颜色从白色更改为黑色,xaml页面中的代码如下所示: <ContentPage.Resources> <ResourceDictionary> <Style x:Key="labelStyle" TargetType="Label"> <S
和一些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,0,0" />
<Setter Property="FontAttributes"
Value="Bold" />
<Setter Property="TextColor"
Value="White"/>
</Style>
<local:LongToDateTimeConverter x:Key="longToDateTimeConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<?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,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>
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;
}
}
}
如何获取
以及如何在c#中将其颜色更改为黑色?没有足够的信息,但我假设您可以使用来实现最终目标,即基于某些数据(属性)动态更改文本颜色
如果您想从MainPage.xaml.cs
更改颜色,可以使用稍微更改的@Cfun建议的建议,而不是在xaml中硬编码值,您可以向Setter.value
添加绑定对象,如下所示:
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="labelStyle"
TargetType="Label">
<Setter Property="FontSize"
Value="Medium" />
<Setter Property="Margin"
Value="0,0,0,0" />
<Setter Property="FontAttributes"
Value="Bold" />
<Setter Property="TextColor"
Value="White"/>
</Style>
<local:LongToDateTimeConverter x:Key="longToDateTimeConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<?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,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>
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;
}
}
}
我添加了isWeatherConditionChecked
和labelStyleTextColor
仅作为示例,但我想您的代码中也有类似的东西可以使用。如果它回答了您的问题或您觉得它很有帮助,请让我知道,否则您可能会给我留下反馈/评论。