如何将手势识别器添加到viewModel C# [英] How can I add a gesture recognizer to a viewModel C#
本文介绍了如何将手势识别器添加到viewModel C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的标签:
<Label Grid.Row="1" Grid.Column="0" x:Name="faveLabel" Style="{StaticResource smallIcon}" FontFamily="FontAwesome" VerticalOptions="FillAndExpand" VerticalTextAlignment="Center" />
在后面的C#中,我有:
and in the C# behind I have:
faveLabel.GestureRecognizers.Add(
new TapGestureRecognizer()
{
Command = new Command(() =>
{
App.phrase.Favorite = !App.phrase.Favorite;
faveLabel.TextColor = App.phrase.Favorite == true ? Color.Red : Color.Gray;
App.DB.UpdateFavorite(App.phrase.Favorite, App.phrase.PhraseId);
})
});
当我将ViewModel绑定到此框架时,如何将其移入此viewModel代码中?
As I am binding a ViewModel to this frame then how can I move this into this viewModel code?
public class PhrasesFrameViewModel : ObservableProperty
{
public PhrasesFrameViewModel()
{
var aButtonClickedCommand = new Command(() =>
{
App.DB.IncrementScore(App.cfs, App.phrase, (int)App.aBtn);
App.correctButtonPressed = (int)App.aBtn;
ResetTimer2();
});
请注意,我真的想避免将手势识别器添加到XAML中,因为XAML已经很大.
Note that I would really like to avoid adding the gesture recognizer into the XAML as the XAML is already rather large.
推荐答案
您可以绑定到最新版本的Xamarin.Forms中的命令,因此可以执行以下操作:
You can bind to a command in the latest versions of Xamarin.Forms, so you can do something like so:
您的视图模型:
public class PhrasesFrameViewModel : ObservableProperty
{
public PhrasesFrameViewModel()
{
var aButtonClickedCommand = new Command(() =>
{
App.DB.IncrementScore(App.cfs, App.phrase, (int)App.aBtn);
App.correctButtonPressed = (int)App.aBtn;
ResetTimer2();
});
您的xaml:
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding aButtonClickedCommand}"/>
</Label.GestureRecognizers>
这应该将视图模型中的命令绑定到标签.
That should bind the command in your viewmodel to your label.
这篇关于如何将手势识别器添加到viewModel C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文