How do we detect double tap/click on a button in UWP?

Abhishek Jakabal 86 Reputation points
2022-06-21T06:49:57.513+00:00

Hello everyone, i have a button in my UWP app. If one time clicked/tapped i want it to perform some operation and if double clicked/tapped, it should perform other operation. How do i do this?

I know, there are some similar queries in this forum, but i felt those are not comprehensive enough. So i've created this question.

Please answer asap and keep things in detail.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2022-06-21T08:59:25.63+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The button object has a DoubleTapped Event which will occur when the user double-taps the button. You could handle that event for your logic.

    One thing you need to notice is that, If you want different logic for Tapped versus DoubleTapped, your Tapped handler may need to use app-specific variables and a timer in order to avoid running on interactions that are eventually interpreted as a DoubleTap action. This is mentioned in the official document here: UIElement.DoubleTapped Event Remarks.

    So what you need to do looks like this:

     <Button Content="Click" Tapped="MyButton_Tapped" IsDoubleTapEnabled="True" DoubleTapped="MyButton_DoubleTapped" Name="MyButton"/>  
    

    Code-behind:

      public bool IsSingleTap;  
      
            private void MyButton_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)  
            {  
                IsSingleTap = false;  
      
                Debug.WriteLine("MyButton_DoubleTapped");  
            }  
      
            private async void MyButton_Tapped(object sender, TappedRoutedEventArgs e)  
            {  
                IsSingleTap = true;  
                await Task.Delay(200);  
                if (IsSingleTap)  
                {  
      
                    Debug.WriteLine("MyButton_Tapped");  
                }  
      
                 
            }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments