Xamarin Forms Pass TapGestureRecognizer CommandParameter

  • by
xamarin-forms-tapped-gesture

TapGestureRecognizer in Xamarin Forms are used to create click events on controls like Labels, Layouts, Frames. While using Tapped gestures we can create each event for each controls, but this will lead to number of different functions in code-behind. But by using CommandParameter, we can parse and recognize controls which is clicked or pass some data directly. Here, we create a simple example for demonstrating the same.

DOWNLOAD SOURCE CODE

Xamarin Forms TapGestureRecognizer CommandParameter Example:

MainPage.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:XamSample"
x:Class="XamSample.MainPage"
Title="Xamarin ListView">

<StackLayout Orientation="Vertical" Padding="5">

<Frame CornerRadius="5" HeightRequest="30" MinimumHeightRequest="30" HorizontalOptions="CenterAndExpand"
Padding="5" BackgroundColor="#166EDC" Margin="5" WidthRequest="100" MinimumWidthRequest="100">

<Label Text="JAVA" TextColor="#fff" FontSize="Medium" HorizontalOptions="Center"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" CommandParameter="Java"/>
</Frame.GestureRecognizers>
</Frame>

<Frame CornerRadius="5" HeightRequest="30" MinimumHeightRequest="30" HorizontalOptions="CenterAndExpand"
Padding="5" BackgroundColor="#5B4DBE" Margin="5" WidthRequest="100" MinimumWidthRequest="100">

<Label Text="Xamarin" TextColor="#fff" FontSize="Medium" HorizontalOptions="Center"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" CommandParameter="Xamarin"/>
</Frame.GestureRecognizers>
</Frame>

<Frame CornerRadius="5" HeightRequest="30" MinimumHeightRequest="30" HorizontalOptions="CenterAndExpand"
Padding="5" BackgroundColor="#21283c" Margin="5" WidthRequest="100" MinimumWidthRequest="100">

<Label Text="Kotlin" TextColor="#fff" FontSize="Medium" HorizontalOptions="Center"/>
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" CommandParameter="Kotlin"/>
</Frame.GestureRecognizers>
</Frame>

</StackLayout>

</ContentPage>

There are three frames used to create the design and Labels showing the names. And on TapGestureRecognizer function we are passing CommandParameter.

Xamarin-Forms-Pass-TapGestureRecognizer-CommandParameter-example

Edit the code-behind as below:

MainPage.xaml.cs:

using System;
using Xamarin.Forms;

namespace XamSample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
try
{
Frame frame = (Frame)sender;
TapGestureRecognizer tapGesture = (TapGestureRecognizer)frame.GestureRecognizers[0];
await DisplayAlert("Info", "You selected " + tapGesture.CommandParameter.ToString(), "Ok");
}
catch(Exception ex)
{
await DisplayAlert("Error", ex.Message.ToString(), "Ok");
}
}
}
}

OUTPUT:
Xamarin-Forms-Pass-TapGestureRecognizer-CommandParameter-example

You can download the source code for free using the link below:

DOWNLOAD SOURCE CODE

Also see:
ListView hide Scrollbars in Xamarin Forms

Button Press CommandParameter – Xamarin Forms

Xamarin forms SQLite DB – Perform CRUD operation.

Xamarin forms SkiaSharp – Creating different shapes

Creating SQLite Database in Xamarin forms

Xamarin forms Creating Custom Button control

 


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.

1 thought on “Xamarin Forms Pass TapGestureRecognizer CommandParameter”

  1. Pingback: Xamarin.Forms MVVM Binding CommandParameter to Command - ParallelCodes

Comments are closed.