web-dev-qa-db-fra.com

Comment faire un geste de presse long dans Xamarin Forms?

Pourriez-vous s'il vous plaît laissez-moi savoir comment puis-je reconnaître un geste long dans l'application Xamarin Forms?

Quelques jours avant, j'ai utilisé TapGestureRecognizer 

TapGestureRecognizer imageTap = new TapGestureRecognizer();
imageTap.Tapped += (sender, args) => this.OnClickImage;
image.GestureRecognizers.Add(imageTap);

Mais je ne sais pas comment faire un long geste de presse en fonction de ceci fil du forum xamarin

Cela devrait ressembler à quelque chose comme ça, mais ça ne marche pas.

var dumpParam = new RelayGesture((g, x) => DisplayAlert("Title", "Hello message", "Cancel"));

book.Cover.SetValue(Gestures.InterestsProperty, new GestureCollection() {
    new GestureInterest
        {
            GestureType = GestureType.LongPress
            GestureCommand = // what should I set?
            GestureParameter = dumpParam
        }
 });

Comment définir ma méthode de gestionnaire personnalisé? 

6
Joseph Katzman

Surfer sur Internet, j'ai trouvé la solution. Il y a peu d'étapes que vous devriez reproduire. 

(1) _ {héritez du contrôle sur lequel vous avez besoin des gestes} (c'est-à-dire si vous voulez ajouter un geste à Xamarin.Forms.Image, créez votre propre classe ImageWithLongPressGesture).

public class ImageWithLongPressGesture : Xamarin.Forms.Image
{
    public EventHandler LongPressActivated;

    public void HandleLongPress(object sender, EventArgs e)
    {
        //Handle LongPressActivated Event
    }
}

2) Exposez les événements publics pour les gestes nécessaires.

3) Créez un moteur de rendu pour chaque plate-forme. 

4) Dans le moteur de rendu, gérez les gestes et affichez-les sous votre contrôle. 

[Assembly: ExportRenderer(typeof(ImageWithLongPressGesture), typeof(LongPressGestureRecognizerImageRenderer))]
namespace App1.Droid.DroidRenderers
{
    public class LongPressGestureRecognizerImageRenderer : ImageRenderer
    {
        ImageWithLongPressGesture view;

        public LongPressGestureRecognizerImageRenderer()
        {
            this.LongClick += (sender, args) => {
                Toast.MakeText(this.Context, "Long press is activated.", ToastLength.Short).Show();
            };
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
        {
            base.OnElementChanged(e);

            if(e.NewElement != null)
            {
                view = e.NewElement as ImageWithLongPressGesture;
            }
        }
    }
}

Cette solution est un hybride de answer sur le forum xamarin forms et Présentation de Touch and Gestures par Telerik. 

3
Joseph Katzman

Utilisez le package de nuget XLabs.Forms, qui permet d’appuyer longuement et d’autres gestes dans le code PCL uniquement ........ L’utilisation du package XLabs.Forms réduira le besoin de rendu personnalisé sur des plates-formes individuelles .... Ajouter un code XAML Fichier .xaml et gestionnaire d’événements attachés dans le fichier .xaml.cs .. Il fonctionne correctement sous Android ..

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.Microsoft.com/winfx/2009/xaml"
         x:Class="MultiImage.Page1"             
         xmlns:lc="clr-namespace:XLabs.Forms.Controls;Assembly=XLabs.Forms"
         xmlns:lb="clr-namespace:XLabs.Forms.Behaviors;Assembly=XLabs.Forms">
<ContentPage.Content>
    <lc:GesturesContentView ExcludeChildren="False" GestureRecognized="GesturesContentView_GestureRecognized">
         <lb:Gestures.Interests>
                <lb:GestureCollection>
                    <lb:GestureInterest GestureType="SingleTap"/>
                    <lb:GestureInterest GestureType="LongPress"/>
                    <lb:GestureInterest GestureType="DoubleTap"/>
                </lb:GestureCollection>
          </lb:Gestures.Interests>
          <Image Source="Myimage.png" Aspect="AspectFit" HeightRequest="100"/>
        </lc:GesturesContentView>
</ContentPage.Content>

Code backend C #: 

private void GesturesContentView_GestureRecognized(object sender, GestureResult e)
{           
    switch (e.GestureType)
    {
        case GestureType.LongPress:
            //Add code here
            break;
        case GestureType.SingleTap:
            // Add code here                    
            break;
        case GestureType.DoubleTap:
            // Add code here
            break;
        default:
            break;
    }
3
shubham aggarwal

Vous pouvez le faire de manière multi-plateforme en attachant le comportement ci-dessous, à condition que ce soit Xamarin.Forms.Button ou un sous-type de celui-ci.

using System;
using System.Threading;
using System.Windows.Input;
using Xamarin.Forms;

namespace App.Controls.Behaviors
{
    public class LongPressBehavior : Behavior<Button>
    {
        private readonly object _syncObject = new object();
        private const int Duration = 1000;

        //timer to track long press
        private Timer _timer;
        //the timeout value for long press
        private readonly int _duration;
        //whether the button was released after press
        private volatile bool _isReleased;

        /// <summary>
        /// Occurs when the associated button is long pressed.
        /// </summary>
        public event EventHandler LongPressed;

        public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command),
            typeof(ICommand), typeof(LongPressBehavior), default(ICommand));

        public static readonly BindableProperty CommandParameterProperty =
            BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(LongPressBehavior));

        /// <summary>
        /// Gets or sets the command parameter.
        /// </summary>
        public object CommandParameter
        {
            get => GetValue(CommandParameterProperty);
            set => SetValue(CommandParameterProperty, value);
        }

        /// <summary>
        /// Gets or sets the command.
        /// </summary>
        public ICommand Command
        {
            get => (ICommand)GetValue(CommandProperty);
            set => SetValue(CommandProperty, value);
        }

        protected override void OnAttachedTo(Button button)
        {
            base.OnAttachedTo(button);
            this.BindingContext = button.BindingContext;
            button.Pressed += Button_Pressed;
            button.Released += Button_Released;
        }

        protected override void OnDetachingFrom(Button button)
        {
            base.OnDetachingFrom(button);
            this.BindingContext = null;
            button.Pressed -= Button_Pressed;
            button.Released -= Button_Released;
        }

        /// <summary>
        /// DeInitializes and disposes the timer.
        /// </summary>
        private void DeInitializeTimer()
        {
            lock (_syncObject)
            {
                if (_timer == null)
                {
                    return;
                }
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
                _timer.Dispose();
                _timer = null;
                Debug.WriteLine("Timer disposed...");
            }
        }

        /// <summary>
        /// Initializes the timer.
        /// </summary>
        private void InitializeTimer()
        {
            lock (_syncObject)
            {
                _timer = new Timer(Timer_Elapsed, null, _duration, Timeout.Infinite);
            }
        }

        private void Button_Pressed(object sender, EventArgs e)
        {
            _isReleased = false;
            InitializeTimer();
        }

        private void Button_Released(object sender, EventArgs e)
        {
            _isReleased = true;
            DeInitializeTimer();
        }

        protected virtual void OnLongPressed()
        {
            var handler = LongPressed;
            handler?.Invoke(this, EventArgs.Empty);
            if (Command != null && Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }
        }

        public LongPressBehavior()
        {
            _isReleased = true;
            _duration = Duration;
        }

        public LongPressBehavior(int duration) : this()
        {
            _duration = duration;
        }

        private void Timer_Elapsed(object state)
        {
            DeInitializeTimer();
            if (_isReleased)
            {
                return;
            }
            Device.BeginInvokeOnMainThread(OnLongPressed);
        }
    }
}

Dans l'interface utilisateur XAML:

 <Button x:Name="MyButton" Text="Long Press Me!">
   <Button.Behaviors>
     <behaviors:LongPressBehavior LongPressed="MyButton_LongPressed"/>
   </Button.Behaviors>
 </Button>

Interface utilisateur XAML avec liaison de commande:

 <Button x:Name="MyButton" Text="Long Press Me!">
   <Button.Behaviors>
     <behaviors:LongPressBehavior Command="{Binding CommandInViewModel}"/>
   </Button.Behaviors>
 </Button>
2
Zafar Ameem
//To Add Programatically:

StackLayout _Containter = new StackLayout();
StackLayout _StackLayout = new StackLayout();
 _StackLayout.Children.Add(new Label(){Text="Execute Me"});

GesturesContentView Gv = new GesturesContentView();
_StackLayout.SetValue(XLabs.Forms.Behaviors.Gestures.InterestsProperty, new GestureCollection() {
                      new GestureInterest() { GestureType = GestureType.SingleTap },
                      new GestureInterest() { GestureType = GestureType.LongPress },
                      new GestureInterest() { GestureType = GestureType.DoubleTap }
            });
Gv.GestureRecognized += Gv_GestureRecognized;
Gv.ExcludeChildren = false;
    Gv.Content = _StackLayout;
_Containter.Children.Add(Gv);
0

Pour que cela fonctionne correctement sur iOS, vous devez utiliser XLabs.Forms.XFormsAppiOS.Init (); dans votre fichier AppDelegate.cs juste avant LoadApplication (new App ()); déclaration.

0
ordertochaos

J'ai récemment rencontré ce problème et trouvé un article utile sur le sujet https://alexdunn.org/2017/12/27/xamarin-tip-xamarin-forms-long-press-effect/

Ceci utilise la variable RoutingEffect et décrit un exemple de création d'implémentation iOS et Android. La simplicité de cette opération vous permet de l’attacher à n’importe quelle vue de votre application sans recréer de code.

0
Richard Ockerby