Xamarin Gesture
Tap Gesture
With the Tap Gesture, you can make any UI-Element clickable (Images, Buttons, StackLayouts, …):
(1) In code, using the event:
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
// handle the tap
};
image.GestureRecognizers.Add(tapGestureRecognizer);
(2) In code, using ICommand
(with MVVM-Pattern, for example):
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.SetBinding (TapGestureRecognizer.CommandProperty, "TapCommand");
image.GestureRecognizers.Add(tapGestureRecognizer);
(3) Or in Xaml (with event and ICommand
, only one is needed):
<Image Source="tapped.jpg">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped" Command="{Binding TapCommand"} />
</Image.GestureRecognizers>
</Image>