如何在 Xamarin Forms 中进行长按手势? [英] How to make long press gesture in Xamarin Forms?

查看:17
本文介绍了如何在 Xamarin Forms 中进行长按手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您告诉我如何在 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;
            }
        }
    }
}

此解决方案是 在 xamarin 表单论坛上回答触摸和手势演示.

This solution is a hybrid of answer on xamarin forms forum and Touch and Gestures presentation by Telerik.

这篇关于如何在 Xamarin Forms 中进行长按手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆