Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Why does a single click also trigger a long press? How to prevent it?

$
0
0

Hello! I want to handle two events. I want to handle a single click to my Xamarin.Forms.Image and a long press. Unfortunately Xamarin Forms does not support long press gesture that I need to realize it for each specific platform. So I inherited from Xamarin.Forms.Image control I need to gesture on.

public class ImageWithLongPressGesture : Image
    {
        public EventHandler longPressActivated;
        public Action longPressAction;

        public void HandleLongPress(object sender, EventArgs e)
        {
            longPressAction();
        }
    }

I know that delegate is a bad solution for that, but I didn't find another solution yet.
It will be used in some part of my code.

foreach (image in images)
                {
                    TapGestureRecognizer imageTap = new TapGestureRecognizer();

                    imageTap.Tapped += (sender, args) =>
                    {
                        this.Navigation.PushAsync(new ContentPage
                        {
                            Content = new Label
                            {
                                Text = "Hello World!"
                            }
                        });
                    };

                    image.GestureRecognizers.Add(imageTap);

                    // Test long press
                    image.longPressAction = () =>
                    {
                        DisplayAlert("Alert", "It should not be display after a single tap.", "Cancel");
                    };
                }

I created a custom renderer for Android platform.

public class LongPressGestureRecognizerImageRenderer : ImageRenderer
    {
        private ImageWithLongPressGesture view;

        /// <summary>
        /// Initialize a new instance of <see cref="LongPressGestureRecognizerImageRenderer"/> class.
        /// </summary>
        public LongPressGestureRecognizerImageRenderer()
        {

        }

        /// <summary>
        /// Notified when an element chnage occurs.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                view = e.NewElement as ImageWithLongPressGesture;
                this.LongClick += this.view.HandleLongPress;
            }
        }
    }

It works good when I press for my image. I get the message "It should not be display after a single tap.". But also I got this message when I click for image one time. How to prevent it?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>