是否可以通过Xamarin.Forms识别屏幕上的短按或长按? [英] Is there a way to recognize a short or long press on a screen with Xamarin.Forms?

查看:532
本文介绍了是否可以通过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天全站免登陆