Atul Dhanuka Atul Dhanuka - 4 months ago 256
Android Question

How to Add Click event in Stack Layout or Frame

I am new in xamarin.forms please help me out how i can add click event in Stack Layout or Frame

<Frame Grid.Column="0" BackgroundColor="#ffffff" Grid.Row="0" HasShadow="true" OutlineColor = "Black">
</Frame>


<StackLayout Grid.Column="0" BackgroundColor="#313FA0" Grid.Row="0">
</StackLayout>

Answer

You can add a TapGestureRecognizer to the StackLayout in XAML like this:

<StackLayout Grid.Column="0" BackgroundColor="#313FA0" Grid.Row="0">
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnTapped"/>
    </StackLayout.GestureRecognizers>
</StackLayout>

Then you can implement the OnTapped method in the code behind:

void OnTapped(object sender, EventArgs e) 
{
    // Do stuff
}

There is a really good guide at the Xamarin website: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/gestures/#Using_Xaml