Disable second tap on Image

ManojReddy.8431ManojReddy.8431 USMember ✭✭
edited November 2015 in Xamarin.Forms

Hi All,

I have an image where i am handling the click event using gestures. Example Below.

XAML CODE

    <Image Source="info.png" HeightRequest="20" WidthRequest="20">
            <Image.GestureRecognizers>
              <TapGestureRecognizer Tapped="OnTap" />
            </Image.GestureRecognizers>
    </Image>

CODE

    private void OnTap(object sender, EventArgs e)
            {
                    var resultTextView = new GenericListView("Result View");
                    Application.Current.MainPage.Navigation.PushAsync(resultTextView);
                }
            }

This works. But if user clicks on the image multiple times, then the tap event is called multiple times. I want it to be called only once.

How do i fix this?

Thank you.

Regards,
Manoj

Best Answer

Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    edited November 2015

    Why don't you make a private boolean variable in your class which flips when the user taps?
    You can also choose to create some sort of timeout with a timer to reset it so after a period of time they can tap again.

    Something like this (not tested or compiled, just for the idea);

      private bool _userTapped;
    
      private void OnTap(object sender, EventArgs e)
      {
         if (_userTapped)
            return;
    
         _userTapped = true;
         var resultTextView = new GenericListView("Result View");
         Application.Current.MainPage.Navigation.PushAsync(resultTextView);
       }
    
  • ManojReddy.8431ManojReddy.8431 USMember ✭✭

    Hi Gerald,

    Thanks for the solution. This sure works.

    But, there is one other thing. When i go back from the Result View to the Parent View, i will not be able to tap the image since we have set the flag _userTapped = true.

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    Another way is a keep a key of the current page in your navigation service and if the call is async you can just check if the page has already been popped onto the nav stack and ignore the 2nd tap, works very well for me.

     public RelayCommand<Order> SelectCommand
            {
                get
                {
                    return _selectCommand
                           ?? (_selectCommand = new RelayCommand<Order>(
                               async ord =>
                               {
                                   try
                                   {
                                       if (!_selectCommand.CanExecute(ord))
                                       {
                                           return;
                                       }
    
                                       // Check if page already loaded to avoid double tap
                                       if (_navService.CurrentPageKey == ViewModelLocator.PageKeyOrdDetails) return;
                                       await _navService.NavigateTo(ViewModelLocator.PageKeyOrdDetails, ord);
                                   }
                                   catch (Exception ex)
                                   {
                                       _exService.Error(GetType().Name, ex);
                                   }
                               },
                               ord => ord != null));
                }
            }
    
  • ManojReddy.8431ManojReddy.8431 USMember ✭✭

    Thanks Adam. Works like a charm :smiley:

  • @adamkemp said:
    You can modify the original suggestion slightly to make it better:

      private bool _userTapped;
    
      private async void OnTap(object sender, EventArgs e)
      {
         if (_userTapped)
            return;
    
         _userTapped = true;
         var resultTextView = new GenericListView("Result View");
         await Application.Current.MainPage.Navigation.PushAsync(resultTextView);
        _userTapped = false;
       }
    

    this worked great, but i put the _userTapped = false in the OnAppearing for when the page reloaded.

  • adamkempadamkemp USInsider, Developer Group Leader mod
    I’m not sure why that would be necessary...
  • @adamkemp worked like a charm for me. thank you!

Sign In or Register to comment.