有没有办法使用 Xamarin.Forms 识别屏幕上的短按或长按? [英] Is there a way to recognize a short or long press on a screen with Xamarin.Forms?

查看:43
本文介绍了有没有办法使用 Xamarin.Forms 识别屏幕上的短按或长按?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以响应屏幕上的短按.我通过添加手势识别器来做到这一点.

I have an application that responds to a short tap on the screen. I do this by adding a gesture recognizer.

有没有办法让它响应短按或长按,并让它们调用不同的方法?

Is there a way that I can make it respond to either a short or a long press and have these call different methods?

推荐答案

您将为此实现渲染器.在 iOS 的情况下,您可以使用 UILongPressGestureRecognizer 来检测长按操作,而在 Android 的情况下,您可以使用 GestureDetector 来执行相同的操作.

You will have implement renderers for that. In case of iOS you can use UILongPressGestureRecognizer to detect a long-press action, while in case of Android, you can use GestureDetector to do the same.

表单控件

public class CustomView : ContentView
{
    public event EventHandler<EventArgs> LongPressEvent;

    public void RaiseLongPressEvent()
    {
        if (IsEnabled)
            LongPressEvent?.Invoke(this, EventArgs.Empty);
    }
}

iOS 渲染器

[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.iOS
{
    public class CustomViewRenderer : ViewRenderer<CustomView, UIView>
    {
        UILongPressGestureRecognizer longPressGestureRecognizer;
        protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
        {
            longPressGestureRecognizer = longPressGestureRecognizer ??
                new UILongPressGestureRecognizer(() =>
                {
                    Element.RaiseLongPressEvent();
                });

            if (longPressGestureRecognizer != null)
            {
                if (e.NewElement == null)
                {
                    this.RemoveGestureRecognizer(longPressGestureRecognizer);
                }
                else if (e.OldElement == null)
                {
                    this.AddGestureRecognizer(longPressGestureRecognizer);
                }
            }
        }
    }
}

Android 渲染器

[assembly: ExportRenderer(typeof(CustomView), typeof(CustomViewRenderer))]
namespace AppNamespace.Droid
{
    public class CustomViewRenderer : ViewRenderer<CustomView, Android.Views.View>
    {
        private CustomViewListener _listener;
        private GestureDetector _detector;

        public CustomViewListener Listener
        {
            get
            {
                return _listener;
            }
        }

        public GestureDetector Detector
        {
            get
            {
                return _detector;
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                GenericMotion += HandleGenericMotion;
                Touch += HandleTouch;

                _listener = new CustomViewListener(Element);
                _detector = new GestureDetector(_listener);
            }
        }

        protected override void Dispose(bool disposing)
        {
            GenericMotion -= HandleGenericMotion;
            Touch -= HandleTouch;

            _listener = null;
            _detector?.Dispose();
            _detector = null;

            base.Dispose(disposing);
        }

        void HandleTouch(object sender, TouchEventArgs e)
        {
            _detector.OnTouchEvent(e.Event);
        }

        void HandleGenericMotion(object sender, GenericMotionEventArgs e)
        {
            _detector.OnTouchEvent(e.Event);
        }
    }

    public class CustomViewListener : GestureDetector.SimpleOnGestureListener
    {
        readonly CustomView _target;

        public CustomViewListener(CustomView s)
        {
            _target = s;
        }

        public override void OnLongPress(MotionEvent e)
        {
            _target.RaiseLongPressEvent();

            base.OnLongPress(e);
        }
    }
}

示例用法

<local:CustomView LongPressEvent="Handle_LongPress" />

代码隐藏

void Handle_LongPressEvent(object sender, System.EventArgs e)
{
    //handle long press event here
}

您也可以在上面自定义添加命令,使其对 MVVM 更加友好.

You can also customize above to add a command to make it more MVVM friendly.

您可以参考此链接了解有关手势识别器的更多详细信息.

You can refer this link for more details regarding gesture recognizers.

这篇关于有没有办法使用 Xamarin.Forms 识别屏幕上的短按或长按?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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