问题描述
能否让我知道如何在Xamarin Forms应用程序中识别长按手势?
Could you please let me know how can I recognize long press gesture in Xamarin Forms application?
使用TapGestureRecognizer
TapGestureRecognizer imageTap = new TapGestureRecognizer();
imageTap.Tapped += (sender, args) => this.OnClickImage;
image.GestureRecognizers.Add(imageTap);
但是我不知道如何根据此来自xamarin论坛的线程
But I don't know how to make long press gesture according to this thread from xamarin forum
它看起来应该像这样,但是不起作用.
It should looks something like this, but it does not work.
var dumpParam = new RelayGesture((g, x) => DisplayAlert("Title", "Hello message", "Cancel"));
book.Cover.SetValue(Gestures.InterestsProperty, new GestureCollection() {
new GestureInterest
{
GestureType = GestureType.LongPress
GestureCommand = // what should I set?
GestureParameter = dumpParam
}
});
如何设置我的自定义处理程序方法?
How to set my custom handler method?
推荐答案
浏览互联网我找到了解决方案.您应该复制几个步骤.
Surfing the internet I found the solution. There are few steps which you should reproduce.
1) 继承需要手势的控件(即,如果要向Xamarin.Forms.Image
添加手势,请创建自己的ImageWithLongPressGesture
类).
1) Inherit the control you need the gestures on (i.e. if you want to add gesture to Xamarin.Forms.Image
, create you own ImageWithLongPressGesture
class).
public class ImageWithLongPressGesture : Xamarin.Forms.Image
{
public EventHandler LongPressActivated;
public void HandleLongPress(object sender, EventArgs e)
{
//Handle LongPressActivated Event
}
}
2)公开公共事件以获取所需的手势.
3)为每个平台创建一个渲染器.
4)在渲染器中,处理手势并将其显示在控件上.
[assembly: ExportRenderer(typeof(ImageWithLongPressGesture), typeof(LongPressGestureRecognizerImageRenderer))]
namespace App1.Droid.DroidRenderers
{
public class LongPressGestureRecognizerImageRenderer : ImageRenderer
{
ImageWithLongPressGesture view;
public LongPressGestureRecognizerImageRenderer()
{
this.LongClick += (sender, args) => {
Toast.MakeText(this.Context, "Long press is activated.", ToastLength.Short).Show();
};
}
protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
{
base.OnElementChanged(e);
if(e.NewElement != null)
{
view = e.NewElement as ImageWithLongPressGesture;
}
}
}
}
此解决方案是和 rel ="nofollow noreferrer">触摸和手势"演示.
This solution is a hybrid of answer on xamarin forms forum and Touch and Gestures presentation by Telerik.
这篇关于如何在Xamarin Forms中做出长按手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!