我有这个XAML代码:
<StackLayout Grid.Row="0" Grid.Column="0" Padding="15,10,20,10" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="tapFavorites" NumberOfTapsRequired="1" />
</StackLayout.GestureRecognizers>
<Label x:Name="faveLabel" FontFamily="FontAwesome" XAlign="Center" FontSize="23">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding Favorite}" Value="true">
<Setter Property="TextColor" Value="Red" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding Favorite}" Value="false">
<Setter Property="TextColor" Value="Gray" />
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
在我的C#代码中,我很熟悉设置标签的Text属性,只需指定如下:
sampleLabel.Text = "ABC"
但这种情况不同。有人可以告诉我如何在点击标签时从C#更改标签的颜色。
答案 0 :(得分:7)
这个怎么样:
<强>的MainPage:强>
public partial class MainPage : ContentPage
{
MyViewModel vm;
public MainPage()
{
InitializeComponent();
vm = new MyViewModel();
BindingContext = vm;
var faveLabel = new Label { FontSize = 24, FontFamily = "FontAwesome", Text = "Tap Here !" };
var trigger1 = new DataTrigger(typeof(Label));
trigger1.Binding = new Binding("Favorite", BindingMode.TwoWay);
trigger1.Value = true;
trigger1.Setters.Add(new Setter { Property = Label.TextColorProperty, Value = Color.Red });
var trigger2 = new DataTrigger(typeof(Label));
trigger2.Binding = new Binding("Favorite", BindingMode.TwoWay);
trigger2.Value = false;
trigger2.Setters.Add(new Setter { Property = Label.TextColorProperty, Value = Color.Gray });
faveLabel.Triggers.Add(trigger1);
faveLabel.Triggers.Add(trigger2);
var sl = new StackLayout {
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
};
var tgr = new TapGestureRecognizer();
tgr.NumberOfTapsRequired = 1;
tgr.Tapped += tapFavorites;
sl.GestureRecognizers.Add(tgr);
sl.Children.Add(faveLabel);
Content = sl;
}
public void tapFavorites(object sender, EventArgs e)
{
vm.Favorite = !vm.Favorite;
}
}
<强>视图模型:强>
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool favorite;
public bool Favorite
{
get { return favorite; }
set
{
if (value != favorite)
{
favorite = value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
答案 1 :(得分:1)
您可以通过它的名称和GestureRecognizer访问您的faveLabel。在那里,您将标签的TextColor设置为另一个。除了在箭头功能之后输入代码,您还可以提供功能。
faveLabel.GestureRecognizers.Add( new TapGestureRecognizer { Command = new Command( () => faveLabel.TextColor = Color.Red ) });
除了在箭头功能之后输入完整代码,您还可以提供功能。
... { Command = new Command(() => OnLabelClicked() ) }
Here是(Tap)GestureRecognizers的官方Xamarin文档的链接。
答案 2 :(得分:1)
如果您在xaml中执行操作,那么触发器就是可行的方法,但如果您在代码中执行此操作,则无论使用哪种方式都可以执行此操作 - 使用或不使用触发器
您可以在模型中保留收藏夹和/或标签文本颜色并进行绑定。如果您有模型,那么您也可以绑定tapFavorites,如Marimba所示
public partial class MainPage : ContentPage
{
Label faveLabel;
bool favorite = false;
public MainPage()
{
InitializeComponent();
faveLabel = new Label { FontSize = 24, FontFamily = "FontAwesome", Text = "Tap Here !" };
var sl = new StackLayout {
HorizontalOptions = LayoutOptions.StartAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand
};
var tgr = new TapGestureRecognizer();
tgr.NumberOfTapsRequired = 1;
tgr.Tapped += tapFavorites;
sl.GestureRecognizers.Add(tgr);
sl.Children.Add(faveLabel);
Content = sl;
}
public void tapFavorites(object sender, EventArgs e)
{
favorite = ! favorite;
faveLabel.TextColor = favorite ? Color.Red : Color.Gray;
}
}