John Livermore
John Livermore

Reputation: 31313

Xamarin Forms - limiting TapGestureRecognizer to a single tap

I have a TapGestureRecognizer on a Label. All works great with the following Command handler (using FreshMVVM PushPageModel which navigates to the next page)...

    public Command OpenDailyEntry
    {
        get
        {
            return new Command(async (e) =>
            {
                await CoreMethods.PushPageModel<DailyEntryPageModel>(null);
            });
        }
    }

The problem is the next screen takes a second or so to appear, and before that happens the user can tap multiple times causing the event to fire multiple times. On Android this results in the same page being opened each time the user taps the label.

Is there a way to prevent this without some global flag that disables the Command after the first tap?

Upvotes: 1

Views: 337

Answers (1)

Mouse On Mars
Mouse On Mars

Reputation: 1132

You could add a second lambda for the CanExecute method of your Command.

If you add a property to your page navigation service, e.g. IsCurrentlyPushingPage, which keeps track of when the pushing to the navigation stack starts and when it ends, you can evaluate this property and deactivate the OpenDailyEntry command temporarily.

public Command OpenDailyEntry
{
    get
    {
        return new Command(async (e) =>
           {
               await CoreMethods.PushPageModel<DailyEntryPageModel>(null);
           },
           () =>
           {
               return CoreMethods.IsCurrentlyPushingPage == false;
           }
        );
    }
}

Upvotes: 3

Related Questions