NumberOfTapsRequired不适用于> 2

Uro*_*ros 2 xamarin xamarin.forms

我有Xamarin Forms项目,在我的页面上有以下代码:

<Image Source="genea_login_logo.png">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LogoCommand}" NumberOfTapsRequired="2" />
    </Image.GestureRecognizers>
</Image>
Run Code Online (Sandbox Code Playgroud)

它按预期工作。但是,当我将NumberOfTapsRequired从2更改为5时,它将不再起作用。这是预期的行为吗?是否可以实现5单击命令?

编辑:这仅在Android上。

Jes*_*ulo 5

实际上,具有超过2个NumberOfTapsRequired的TapGestureRecognizer在Android中实际上不起作用。您可以实现某种逻辑来实现此效果。

在XAML中

<?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="Demo.Views.MainPage"             
             Title="MainPage">
    <Grid HorizontalOptions="Center" VerticalOptions="Center">       

        <Image Source="icon.png">
            <Image.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped"/>
            </Image.GestureRecognizers>
        </Image>

    </Grid>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

后面的代码...

    private DateTime? LastTap = null;
    private byte NumberOfTaps = 0;

    private const int NumberOfTapsRequired = 3;
    private const int ToleranceInMs = 1000;

    private async void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
    {
        if (LastTap ==  null || (DateTime.Now - LastTap.Value).TotalMilliseconds < ToleranceInMs)
        {
            if (NumberOfTaps == (NumberOfTapsRequired - 1))
            {

                await App.Current.MainPage.DisplayAlert("Hi", "Hi from Gesture", "Ok");

                NumberOfTaps = 0;
                LastTap = null;
                return;
            }
            else
            {
                NumberOfTaps++;
                LastTap = DateTime.Now;
            }
        }
        else
        {
            NumberOfTaps=1;
            LastTap = DateTime.Now;
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • “ ...-LastTap.Value).Milliseconds”应为“ ...-LastTap.Value).TotalMilliseconds” (2认同)
  • 感谢您与我们分享您的解决方案。我偶然发现了同样的问题,在 Android 上点击两次以上是行不通的......所以我正在实施类似于你的解决方案的东西。:-) (2认同)