我在Xamarin Forms上遇到过这个问题(在Android和iOS上测试过)。
我有一个简单的页面
using System;
using Xamarin.Forms;
namespace BugTGR
{
public class PageMain : ContentPage
{
public PageMain()
{
PageMainViewModel vm = new PageMainViewModel();
this.BindingContext = vm;
Label label1 = new Label{ Text = "Press with ICommand"};
TapGestureRecognizer tgr = new TapGestureRecognizer();
tgr.BindingContext = vm;
tgr.SetBinding(TapGestureRecognizer.CommandProperty, "Tapped");
label1.GestureRecognizers.Add(tgr);
Label label2 = new Label { Text = "Press with Tapped"};
TapGestureRecognizer tgr1 = new TapGestureRecognizer();
tgr1.Tapped += async (object sender, EventArgs e) => {
await DisplayAlert("Attention", "PRESSED WITH TAPPED", "Ok");
};
label2.GestureRecognizers.Add(tgr1);
Content = new StackLayout
{
Children = {label1, label2}
};
}
}
}
在这段代码中,我使用了这个ViewModel(非常简单,只有一个命令)
using System;
using System.Windows.Input;
using Xamarin.Forms;
using PropertyChanged;
namespace BugTGR
{
[ImplementPropertyChanged]
public class PageMainViewModel
{
public PageMainViewModel()
{
this.Tapped = new Command(async() =>
{
await Application.Current.MainPage.DisplayAlert("Attention", "Pressed", "Ok");
});
}
public ICommand Tapped { protected get; set;}
}
}
然后,你怎么看,我尝试将命令绑定到TapGestureRecognizer,然后将TGR添加到标签,但如果我单击标签,则不会调用该命令。
在第二个标签(label2)中,我使用Tapped事件添加另一个TapGestureRecognizer而不绑定命令。这有效!
有人可以让我知道我做错了什么?
谢谢! 的Alessandro
答案 0 :(得分:3)
这是工作解决方案。问题是如何创建Tapped命令。 以下是两种方法。调用VM的命令或事件处理程序。如果你在代码而不是在xaml中这样做我会使用vm.TappedHandler方法
namespace ButtonRendererDemo
{
public class LabelTapPage : ContentPage
{
public LabelTapPage()
{
PageMainViewModel vm = new PageMainViewModel();
this.BindingContext = vm;
Label label1 = new Label { Text = "Press with ICommand" };
TapGestureRecognizer tgr = new TapGestureRecognizer();
label1.GestureRecognizers.Add(tgr);
tgr.SetBinding(TapGestureRecognizer.CommandProperty, "Tapped");
Label label2 = new Label { Text = "Press with Tapped Event" };
TapGestureRecognizer tgr1 = new TapGestureRecognizer();
tgr1.Tapped += async (object sender, EventArgs e) => {
await DisplayAlert("Attention", "Tapped Event: Pressed", "Ok");
};
label2.GestureRecognizers.Add(tgr1);
Label label3 = new Label { Text = "Press with TappedHandler" };
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += async (s, e) => {
await vm.TappedHandler();
};
label3.GestureRecognizers.Add(tapGestureRecognizer);
Content = new StackLayout
{
Children = { label1, label2, label3 }
};
}
}
public class PageMainViewModel : INotifyPropertyChanged
{
ICommand tapCommand;
public PageMainViewModel()
{
tapCommand = new Command(OnTapped);
}
public async Task TappedHandler()
{
await Application.Current.MainPage.DisplayAlert("Attention", "TappedHandler: Pressed", "Ok");
}
public ICommand Tapped
{
get { return tapCommand; }
}
async void OnTapped(object s)
{
await Application.Current.MainPage.DisplayAlert("Attention", "Tapped Command: Pressed", "Ok");
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
此外,您不需要设置tgr.BindingContext = vm;它由您的页面继承
在我发布之后,我发现有更简单的解决方案:
删除
中的“受保护”public ICommand Tapped { get; set; }
因此页面可以访问它。就是这样:-)
可能是你的意思是让“set”受保护而不是得到?