You will have to implement visualization tools for this. In the case of iOS, you can use the UILongPressGestureRecognizer to detect long-click actions, while in the case of Android, you can use the GestureDetector to do the same.
Form management
public class CustomView : ContentView { public event EventHandler<EventArgs> LongPressEvent; public void RaiseLongPressEvent() { if (IsEnabled) LongPressEvent?.Invoke(this, EventArgs.Empty); } }
iOS rendering
[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); } } } } }
rendering for 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); } } }
Sample use
<local:CustomView LongPressEvent="Handle_LongPress" />
Code for
void Handle_LongPressEvent(object sender, System.EventArgs e) {
You can also customize above to add a command to make it more MVVM friendly.
You can refer to this link for more information about gesture recognizers.
Ada
source share