Is there a way to recognize short or long print on the screen using Xamarin.Forms?

I have an application that responds to a short press on the screen. I do this by adding a gesture recognizer.

Is there a way by which I can respond to a short or long press and use these methods?

+7
xamarin xamarin.forms
source share
2 answers

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) { //handle long press event here } 

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.

+4
source share

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

You will have to implement visualization tools for this. In case ios and android

best way to do it!

0
source share

All Articles