Xamarin 实现 Button LongClick 和 Touch

Xamarin.Forms 中默认是不实现这两个功能的,这就需要你在 Renderer 中自己去实现,下面是关于如何在 Xamarin.Android 中实现这个功能的代码:

Forms 部分代码

public interface IMyButtonController : IViewController
{
    void SendTouched();

    void SendLongClicked();

    void SendReleased();
}
public class MyButton : Xamarin.Forms.Button, IMyButtonController
{
    public event EventHandler Touched;

    void IMyButtonController.SendTouched()
    {
        Touched?.Invoke(this, EventArgs.Empty);
    }

    public event EventHandler LongClicked;

    void IMyButtonController.SendLongClicked()
    {
        LongClicked?.Invoke(this, EventArgs.Empty);
    }

    public event EventHandler Released;

    void IMyButtonController.SendReleased()
    {
        Released?.Invoke(this, EventArgs.Empty);
    }
}

在 Xamarin.Android 中的 Renderer:

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))]
namespace ProjectNameSpace.Droid
{
    public class MyButtonRenderer : Xamarin.Forms.Platform.Android.ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                if (Control != null)
                {
                    Control.SetOnTouchListener(ButtonTouchListener.Instance.Value);
                    Control.LongClickable = true;
                    Control.SetOnLongClickListener(ButtonLongClickListener.Instance.Value);
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Control != null)
                {
                    Control.SetOnTouchListener(null);
                    Control.SetOnLongClickListener(null);
                }
            }

            base.Dispose(disposing);
        }

        private class ButtonTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener
        {
            public static readonly Lazy<ButtonTouchListener> Instance = new Lazy<ButtonTouchListener>(() => new ButtonTouchListener());

            public bool OnTouch(Android.Views.View v, Android.Views.MotionEvent e)
            {
                var renderer = v.Tag as ButtonRenderer;
                if (renderer != null)
                {
                    var buttonController = renderer.Element as IMyButtonController;
                    if (e.Action == Android.Views.MotionEventActions.Down)
                    {
                        buttonController?.SendTouched();
                    }
                    else if (e.Action == Android.Views.MotionEventActions.Up)
                    {
                        buttonController?.SendReleased();
                    }
                }
                return false;
            }
        }

        private class ButtonLongClickListener : Java.Lang.Object, Android.Views.View.IOnLongClickListener
        {
            public static readonly Lazy<ButtonLongClickListener> Instance = new Lazy<ButtonLongClickListener>(() => new ButtonLongClickListener());

            public bool OnLongClick(Android.Views.View v)
            {
                var renderer = v.Tag as ButtonRenderer;
                ((IMyButtonController)renderer?.Element)?.SendLongClicked();
                return true;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值