Search code examples
imagexamarin.formsgesture-recognition

Xamarin Forms: How to add single and double tap for an image?


I am trying to implement a keypad UI. In the keypad, the +(plus) and 0(zero) options are placed on a single image icon. So I need to show 0 on UI for single tap and for double tap I need to show the + on UI.

My Code

<Image 
    Grid.Column="1"
    Source="ic_zero_xx.png"
    Style="{StaticResource KeypadImageStyle}">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
            Tapped="ZeroTapped"
            NumberOfTapsRequired="1">
        </TapGestureRecognizer>
    </Image.GestureRecognizers>
</Image>

private void ZeroTapped(object sender, EventArgs e)
{
    phonenumber_label.Text = "0";
}

enter image description here

How I can mention 2 different taps at the same time on an image?


Solution

  • We can add two TapGestureRecognizer on that image, one for single tap and the other for double tap .

    xaml

     <Image >
         <Image.GestureRecognizers>
             <TapGestureRecognizer Tapped="TapGestureRecognizer_Single" NumberOfTapsRequired="1"/>
             <TapGestureRecognizer Tapped="TapGestureRecognizer_Double" NumberOfTapsRequired="2"/>
         </Image.GestureRecognizers>
     </Image>
    

    Code behind

      private void TapGestureRecognizer_Single(object sender, EventArgs e)
      {
          label.Text = "0";
      }
    
      private void TapGestureRecognizer_Double(object sender, EventArgs e)
      {
          label.Text = "+";
      }