点击手势
使用 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>