Share via

Run existing TapGestureRecognizer from code behind

Anonymous
2022-01-10T20:42:48.097+00:00

Hi,

I have the following Label in Xamarin. app

<Label x:Name="LabelBack" Text="Back" Grid.Column="1" TextColor="Blue" Padding="5" HorizontalOptions="Center" VerticalOptions="Center">
<Label.GestureRecognizers>
    <TapGestureRecognizer Tapped="TapGestureRecognizerBack_Tapped" />
</Label.GestureRecognizers>
</Label>

and in my codebehind I want to invoke the TapGestureRecognizerBack_Tapped from another button.

I tried:

TapGestureRecognizerBack_Tapped(null, null);

but did not work

Kindly help.

Thanks,
Jassim

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-01-11T01:54:44.373+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can move code from TapGestureRecognizerBack_Tapped to new method like following code. Then your Tapped event for label could invoke it, your button click could invoke it as well.

       private void TapGestureRecognizerBack_Tapped(object sender, EventArgs e)  
               {  
                   DoSomething();  
               }  
         
               private void DoSomething()  
               {  
                   //Move your tap Gesture code here.  
                   Console.WriteLine("Tapped");  
               }  
               private void Button_Clicked(object sender, EventArgs e)  
               {  
                   DoSomething();  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.