博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Xamarin 实现 Button LongClick 和 Touch
阅读量:4286 次
发布时间:2019-05-27

本文共 3211 字,大约阅读时间需要 10 分钟。

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
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
Instance = new Lazy
(() => 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
Instance = new Lazy
(() => new ButtonLongClickListener()); public bool OnLongClick(Android.Views.View v) { var renderer = v.Tag as ButtonRenderer; ((IMyButtonController)renderer?.Element)?.SendLongClicked(); return true; } } }}

转载地址:http://kepgi.baihongyu.com/

你可能感兴趣的文章
论文笔记 | Attention Is All Y ou Need for Chinese Word Segmentation
查看>>
论文笔记|DOC: Deep Open Classification of Text Documents
查看>>
论文笔记|Undersensitivity in Neural Reading Comprehension
查看>>
论文笔记丨FewRel 2.0: Towards More Challenging Few-Shot Relation Classification
查看>>
论文笔记 _ Discourse-Aware Neural Extractive Text Summarization
查看>>
论文笔记 | Simple and Effective Text Matching with Richer Alignment Features
查看>>
论文笔记:A Gated Self-attention Memory Network for Answer Selection
查看>>
论文笔记 | Simplify the Usage of Lexicon in Chinese NER
查看>>
论文解读 | Unsupervised Data Augmentation for Consistency Training
查看>>
论文笔记|Distantly Supervised Named Entity Recognition using Positive-Unlabeled Learning
查看>>
论文笔记:Span-Based Event Coreference Resolution
查看>>
NAACL2021阅读理解论文整理
查看>>
论文笔记 | Leveraging Graph to Improve Abstractive Multi-Document Summarization
查看>>
NAACL2021丨Knowledge Guided Metric Learning for Few-Shot Text Classification
查看>>
论文笔记|Deep Open Intent Classification with Adaptive Decision Boundary
查看>>
【论文笔记】
查看>>
论文笔记_Pay Attention to MLPs
查看>>
【论文笔记】
查看>>
论文笔记
查看>>
论文笔记 | Attention-based LSTM for Aspect-level Sentiment Classification
查看>>