Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Tap gesture is not working custom controls in Xamarin forms

$
0
0

I am trying to create Custom Label control to handle the Long Click Functionality. I am very much successful to do that. But I am facing problem with Tap gesture recognizer. Tap is not working Custom Label Control

Custom Control Class:

` public class CustomLabel:Label{
        public event EventHandler LongClicked;
        public void OnLongClicked()
        {
            if (LongClicked != null)
                LongClicked(this, new EventArgs());
        }
    }`

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:App2"
             x:Class="App2.MainPage">
    <StackLayout Orientation="Vertical" Spacing="20">
        <Label Text="Welcome to Xamarin" HeightRequest="40">
            <Label.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
            </Label.GestureRecognizers>
        </Label>
        <local:CustomLabel HeightRequest="40" Text="Custom Label Control" LongClicked="CustomLabel_LongClicked" TextColor="Black">
            <local:CustomLabel.GestureRecognizers>
                <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_1"></TapGestureRecognizer>
            </local:CustomLabel.GestureRecognizers>
        </local:CustomLabel>
    </StackLayout>
</ContentPage>`

MainPage.xaml.cs

public partial class MainPage : ContentPage
  {

        public MainPage()
        {
            InitializeComponent();
        }

        private void CustomLabel_LongClicked(object sender, EventArgs e)
        {
            DisplayAlert("From Custom Label", "Log Click Alert from Custom Label", "OK");
        }

        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            DisplayAlert("From Label", "Alert from Label", "OK");

        }

        private void TapGestureRecognizer_Tapped_1(object sender, EventArgs e)
        {
            DisplayAlert("From Custom Label", "Alert from Custom Label", "OK");

        }
    }`

I am doing same procedure to add Tap gesture recognizer to custom label. Tap is working for Predefined Lable but not Custom Label.

Please can you suggest what i am doing wrong with this.
Any kind of suggestions or solutions are most welcome.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>