點選手勢

使用 Tap Gesture,你可以使任何 UI 元素可點選(影象,按鈕,StackLayouts,…):

(1)在程式碼中,使用事件:

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};
image.GestureRecognizers.Add(tapGestureRecognizer);

(2)在程式碼中,使用 ICommand( 例如 MVVM-Pattern ):

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.SetBinding (TapGestureRecognizer.CommandProperty, "TapCommand");
image.GestureRecognizers.Add(tapGestureRecognizer);

(3)或者在 Xaml 中(使用 event 和 ICommand,只需要一個):

<Image Source="tapped.jpg">
    <Image.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnTapGestureRecognizerTapped" Command="{Binding TapCommand"} />
  </Image.GestureRecognizers>
</Image>