GestureRecognizers In Xamarin (Cross Platform) With Image Example

What is GestureRecognizers in Xamarin ?

iOS and Android apps both provide a way to wire up a UI control to listen for the specific touch events. These events are called gestures. Typically, we would use this to listen for a tap (click) gesture.

Step 1

Go to Visual Studio.

Click File -> New -> Project.

Click C# -> Cross Platform -> Cross Platform app (Xamarin.Forms.Portable).

Enter the Application name, followed by clicking OK.

 

Step 2

In this step, go to Solution Explorer -> Portable Class Library, followed by clicking XAML. Insert the code given below XAML page and save it.

 

Step 3

In this step, put an image in Android and iOS folder (shown in the image).

For WinPhone click winphone Project and paste the image.

Step 4

Open App.Xaml.cs and set the page name.

  1. public App()  
  2.   
  3. {  
  4.   
  5. InitializeComponent();  
  6.   
  7. MainPage = new Page1();  
  8.   
  9. }  

Step 5

Open Page1.Xaml and add the code in this page.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="ImageExample.Page1"
  5.              Padding=""20,10,0,0>  
  6.   <ContentPage.Content>  
  7.     <Grid Grid.Row="0" >  
  8.       <Grid.ColumnDefinitions>  
  9.         <ColumnDefinition Width="4*"/>  
  10.       </Grid.ColumnDefinitions>  
  11.       <Grid.RowDefinitions>  
  12.         <RowDefinition Height="130"></RowDefinition>  
  13.         <RowDefinition Height="100"></RowDefinition>  
  14.       </Grid.RowDefinitions>  
  15.       <Image  x:Name="imgmain"  Source="Chrysanthemum.jpg" Grid.Row="0" Grid.Column="0" HorizontalOptions="Start" VerticalOptions="Start"  Margin="20,0,0,0"  />  
  16.       <Grid  Grid.Row="1">  
  17.         <Grid.ColumnDefinitions>  
  18.           <ColumnDefinition Width="80"/>  
  19.           <ColumnDefinition Width="80"/>  
  20.           <ColumnDefinition Width="80"/>  
  21.         </Grid.ColumnDefinitions>  
  22.         <Grid.RowDefinitions>  
  23.           <RowDefinition Height="100"/>  
  24.         </Grid.RowDefinitions>  
  25.         <Image Source="Desert.jpg" HeightRequest="40" x:Name="img1" Grid.Column="0"   />  
  26.         <Image Source="Hydrangeas.jpg" HeightRequest="40" x:Name="img2" Grid.Column="1"   />  
  27.         <Image Source="Jellyfish.jpg" HeightRequest="40" x:Name="img3" Grid.Column="2"/>  
  28.       </Grid>  
  29.     </Grid>  
  30.   </ContentPage.Content>  
  31. </ContentPage>  

Step 6

Open Page1.Xaml.cs and add the code in this page.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. using Xamarin.Forms;  
  8.   
  9. namespace ImageExample  
  10. {  
  11.     public partial class Page1 : ContentPage  
  12.     {  
  13.         public Page1()  
  14.         {  
  15.             InitializeComponent();  
  16.             img1.GestureRecognizers.Add((new TapGestureRecognizer((view) => OnImageClicked1())));  
  17.             img2.GestureRecognizers.Add((new TapGestureRecognizer((view) => OnImageClicked2())));  
  18.             img3.GestureRecognizers.Add((new TapGestureRecognizer((view) => OnImageClicked3())));  
  19.         }  
  20.         public void OnImageClicked3()  
  21.         {  
  22.             imgmain.Source = img3.Source;  
  23.         }  
  24.   
  25.         public void OnImageClicked2()  
  26.         {  
  27.             imgmain.Source = img2.Source;  
  28.         }  
  29.   
  30.         public void OnImageClicked1()  
  31.         {  
  32.             imgmain.Source = img1.Source;  
  33.         }  
  34.     }  
  35. }  

Step 7

Click Build menu and go to Configuration Manager.

Configure your Android and Windows.

, then Click Close.

Step 8

In this step, select Build & Depoly option, followed by clicking to Start your Application.

Now, go to Run option, choose Debug from the list of Android or iOS or UWP Simulators, which are available. You can choose any one Simulator and run it.

Step 9

Output

After few seconds, the app will start running on your Android Simulator. You will see your app is working successfully.

.

Now, click one by one all the images.

  

Up Next
    Ebook Download
    View all
    Learn
    View all