Search code examples
xamarinxamarin.formsgesturerenderer

LongPressGestureRecognizer in Xamarin.Forms


I am applying gesture feature to my Label control. I have used this link- http://arteksoftware.com/gesture-recognizers-with-xamarin-forms/

I was able to get the LongPressGestureRecognizer event when i performed long press on label control.This event is getting called in renderer file.

I want to perform some operation in my shared code on LongPressGestureRecognizer event. so how can I detect this event in my shared code ? How to handle event handler to get this longpress event in my shared code ?


Solution

  • In your custom control declare command:

    public class TappedGrid : Grid
    {
        public static readonly BindableProperty TappedCommandProperty =
            BindableProperty.Create(nameof(TappedCommand),
                            typeof(ICommand),
                            typeof(TappedGrid),
                            default(ICommand));
    
        public ICommand TappedCommand
        {
            get { return (ICommand)GetValue(TappedCommandProperty); }
            set { SetValue(TappedCommandProperty, value); }
        }
    
        public static readonly BindableProperty LongPressCommandProperty =
            BindableProperty.Create(nameof(LongPressCommand),
                                    typeof(ICommand),
                                    typeof(TappedGrid),
                                    default(ICommand));
    
        public ICommand LongPressCommand
        {
            get { return (ICommand)GetValue(LongPressCommandProperty); }
            set { SetValue(LongPressCommandProperty, value); }
        }
    }
    

    And then raise this command from renderer:

    public class TappedGridRenderer : ViewRenderer
    {
        UITapGestureRecognizer tapGesturesRecognizer;
        UILongPressGestureRecognizer longPressGesturesRecognizer;
    
        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            base.OnElementChanged(e);
    
            tapGesturesRecognizer = new UITapGestureRecognizer(() =>
            {
                var grid = (TappedGrid)Element;
                if (grid.TappedCommand.CanExecute(Element.BindingContext))
                {
                    grid.TappedCommand.Execute(Element.BindingContext);
                }
            });
    
            longPressGesturesRecognizer = new UILongPressGestureRecognizer(() =>
            {
                var grid = (TappedGrid)Element;
                if (longPressGesturesRecognizer.State == UIGestureRecognizerState.Ended &&
                        grid.LongPressCommand.CanExecute(Element.BindingContext))
                {
                    grid.LongPressCommand.Execute(Element.BindingContext);
                }
            });
    
            this.RemoveGestureRecognizer(tapGesturesRecognizer);
            this.RemoveGestureRecognizer(longPressGesturesRecognizer);
    
            this.AddGestureRecognizer(tapGesturesRecognizer);
            this.AddGestureRecognizer(longPressGesturesRecognizer);
        }
    }