有没有办法用Xamarin.Forms识别屏幕上的短按或长按?

时间:2017-10-20 08:53:48

标签: xamarin xamarin.forms

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

有没有办法可以让它对短按或长按做出反应并让这些方法调用不同的方法?

2 个答案:

答案 0 :(得分:5)

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

表单控件

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。

您可以参考this link了解有关手势识别器的详细信息。

答案 1 :(得分:0)

http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/

您将为此实现渲染器。如果是ios和android

这样做的最好方法!