Xamarin 如何使用命令而不是事件在类中创建名为方法的TapGestureRecognitor?
我有以下代码:Xamarin 如何使用命令而不是事件在类中创建名为方法的TapGestureRecognitor?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有以下代码: public ScrollHeadingView2() { outerGrid = new Grid() { BackgroundColor = Color.Red, RowSpacing = 0, ColumnSpacing = 0 }; outerGrid.AddChild(new BoxView() { HeightRequest = 100, WidthRequest = 100, BackgroundColo
public ScrollHeadingView2()
{
outerGrid = new Grid() { BackgroundColor = Color.Red, RowSpacing = 0, ColumnSpacing = 0 };
outerGrid.AddChild(new BoxView()
{
HeightRequest = 100,
WidthRequest = 100,
BackgroundColor = Color.Pink
}, 0, 0);
var tap1 = new TapGestureRecognizer()
{
NumberOfTapsRequired = 1
}
tap1.Tapped += async (s, o) => { await BackArrowTapped(); };
grid1.GestureRecognizers.Add(tap1);
}
private async Task BackArrowTapped()
{
await Shell.Current.Navigation.PopAsync(false);
}
我正在使用一个事件尝试这种方法,但它似乎触发了两次。有人能告诉我如何用调用backarrowtaped()的命令来替换事件吗?在 然后
tap1.SetBinding(TapGestureRecognizer.CommandProperty, "TapCommand");
ICommand tapCommand;
public TapViewModel () {
// configure the TapCommand with a method
tapCommand = new Command (OnTapped);
}
public ICommand TapCommand {
get { return tapCommand; }
}
void OnTapped (object s) {
taps++;
Debug.WriteLine ("parameter: " + s);
}