NumberOfTapsRequired不适用于> 2

时间:2017-05-16 13:57:03

标签: xamarin xamarin.forms

我有Xamarin Forms项目,在我的页面上有这样的代码:

<Image Source="genea_login_logo.png">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LogoCommand}" NumberOfTapsRequired="2" />
    </Image.GestureRecognizers>
</Image>

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

编辑:这只适用于Android。

3 个答案:

答案 0 :(得分:4)

  

具有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>

代码背后......

    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).Milliseconds < 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;
        }
    }

答案 1 :(得分:0)

    long lastPress;
    private DateTime? LastTap = null;
    private int NumberOfTaps = 0;
    private const int NumberOfTapsRequired = 4;
    private const long ToleranceInMs = 1000;
    var logoTapRecognizer = new TapGestureRecognizer();
    logoTapRecognizer.Tapped += async(s, e) =>
    {
               long currentTime = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
               if (currentTime - lastPress > ToleranceInMs)
               {
                    lastPress = currentTime;
                    NumberOfTaps = 1;
               }
               else
               {
                    NumberOfTaps++;
                    if(NumberOfTaps == NumberOfTapsRequired)
                          await App.Current.MainPage.DisplayAlert("Hi", "Hi from Gesture", "Ok");
               }
    };
    logo.GestureRecognizers.Add(logoTapRecognizer);

答案 2 :(得分:0)

我修改了耶稣的解决方案,使它也能识别一次轻击事件:

    public MyPage()
    {
        this.InitializeComponent();

        myControl.Tapped += async (sender, e) => 
        {
            await OnTappedAsync(sender, e);
        };

        ...
    }

    private DateTime? LastTap = null;
    private volatile byte NumberOfTaps = 0;  // volatile keyword necessary because of multi-threading
    private const int NumberOfTapsRequired = 2;
    private const int ToleranceInMs = 500;
    private async Task OnTappedAsync(object sender, EventArgs e)
    {
        if (LastTap == null || (DateTime.Now - LastTap.Value).TotalMilliseconds < ToleranceInMs)
        {
            if (NumberOfTaps == (NumberOfTapsRequired - 1))     // multi-tap?
            {
                NumberOfTaps = 0;
                LastTap = null;

                await App.Current.MainPage.DisplayAlert("Hi", "Double-tap hi from Gesture", "Ok");
                return;
            }
            else    // necessary number of taps not yet reached
            {
                NumberOfTaps++;
                LastTap = DateTime.Now;
            }
        }
        else
        {
            NumberOfTaps = 1;
            LastTap = DateTime.Now;
        }

        _ = Task.Factory.StartNew(async () =>
        {
            await Task.Delay(ToleranceInMs);    // check if there is still only one tap after latency
            if (NumberOfTaps == 1)
            {
                NumberOfTaps = 0;
                LastTap = null;
                Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
                {
                    await App.Current.MainPage.DisplayAlert("Hi", "Single-tap hi from Gesture", "Ok");
                });
            }
        });
    }