Tapped Gesture in Xamarin Forms

Gestures:

Gesture recognizers can be used to detect user interaction with many elements of a Xamarin.Forms application.

Xamarin.Forms support three different types of recognizers which are:

  • Pinch
  • Pan
  • Tap

In this article we are only covering Tap gesture recognizer.

 

Introduction To Article:

In xamarin froms you have an option to detect tap gesture of user and perform actions on it. In this article we are going to detect tap gesture and perform actions on it.

TappedImage

 

Xamarin forms gives us events and properties for this which are used to make it more perfect.

  • Number of taps required

This is a property used to specify number of taps done by a user. you have to give integer value in it.

  • Tapped

Tapped is an event and this event is called when given number of taps are tapped by a user.

Example:

let’s go through a simple example in which user can tap on an image and alert is displayed.

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"
 xmlns:local="clr-namespace:XamarinWebSampleProjects"
 x:Class="XamarinWebSampleProjects.MainPage">

<StackLayout HorizontalOptions="Center"
 VerticalOptions="Center">
 <Image Source="[email protected]" WidthRequest="200" HeightRequest="200">
 <Image.GestureRecognizers>
 <TapGestureRecognizer Tapped="Image_Tapped" NumberOfTapsRequired="2"></TapGestureRecognizer>
 </Image.GestureRecognizers>
 </Image>
 </StackLayout>

</ContentPage>

CODE:

 private async void Image_Tapped(object sender, EventArgs e)
 {
 await DisplayAlert("Alert", "You Tappped on me", "OK");
 }

OUTPUT:

Tap image output

And when you tap on image alert is generated.

Tap image output

XamarinWeb Founder | MVP – C-SharpCorner | Xamarin Forms Developer

Leave a Reply

Your email address will not be published. Required fields are marked *