Cambaru
Cambaru

Reputation: 93

Label TapGesture doesn't fire Xamarin Forms

I was trying to add a TapGestureRecognizer to a Label.

 SliderAbout.GestureRecognizers.Add(new TapGestureRecognizer
 {
     Command = new Command(() => OpenAboutAppAsync()),
 });

SliderAbout is my Label which is set in Xaml and works correctly.

 <ScrollView Grid.Row="1">
                <Grid VerticalOptions="Start" HorizontalOptions="Start" Margin="20,20,0,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="60"/>
                    </Grid.RowDefinitions>

                    <Label x:Name="SliderAbout" 
                           Text="Über die APP" 
                           Grid.Row="2"  
                           TextColor="White" 
                           FontFamily="Open Sans Light" 
                           FontSize="Medium"/>    
                </Grid>
            </ScrollView>

The code also gets run ( I put it in the class constructor)

But the Method doesn't fire when I tap the label... Why won't it fire?

Upvotes: 2

Views: 4381

Answers (1)

FabriBertani
FabriBertani

Reputation: 1636

You can add the gesture recognizer directly to the xaml and the action in the code behind or the view model if you're using mvvm approach. In your case it would be something like this:

<Label x:Name="SliderAbout" Text="Über die APP" Grid.Row="2"  TextColor="White" FontFamily="Open Sans Light" FontSize="Medium">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnLabelTapped" />
    </Label.GestureRecognizers>
</Label>

And in your code behind

private void OnLabelTapped (object sender, EventArgs e)
{
    //Your code here
}

Upvotes: 2

Related Questions