1

I want any Nuget package or any custom component for Long press event which I want to use in listview for all 3 platforms .I have got a solution regarding Android and iOS but didn't find any solution for UWP. Can anybody help me out of this? Thanks in advance

Anatoly
  • 20,799
  • 3
  • 28
  • 42
Adit Kothari
  • 421
  • 3
  • 16
  • 5
    Possible duplicate of [How to make long press gesture in Xamarin Forms?](https://stackoverflow.com/questions/43569515/how-to-make-long-press-gesture-in-xamarin-forms) – FreakyAli May 23 '19 at 10:45
  • 1
    You can use the plugin MultiGestureView which enables you to use LongPress, Tap and Right Click on different platforms: https://www.nuget.org/packages/Xam.Plugin.MultiGestureView/ – Sparsha Bhattarai May 23 '19 at 16:14
  • Also, you might be able to just use [ContextActions](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/interactivity) on the viewcell of your listView instead of creating a long press gesture. – Nick Peppers May 23 '19 at 16:59

1 Answers1

0

!!! Touch can produce a Holding action, but mouse devices generally can't. https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.holding?view=winrt-19041

PLC class:

  namespace MyNamespace.Effects
{    
    /// <summary>
    /// Эффект длительного нажатия на элемент управления (общая реализация)
    /// </summary>
    public class LongPressedEffect : RoutingEffect
    {
        public LongPressedEffect() : base("LongPressedEffect")
        {
        }
    #region LongPressed

    public static readonly BindableProperty LongPressedCommandProperty = BindableProperty.CreateAttached("LongPressedCommand", typeof(ICommand), 
        typeof(LongPressedEffect), null);


    public static ICommand GetLongPressedCommand(BindableObject view)
    {
        return (ICommand)view.GetValue(LongPressedCommandProperty);
    }

    public static void SetLongPressedCommand(BindableObject view, ICommand value, object parameter)
    {
        view.SetValue(LongPressedCommandProperty, value);
        SetLongPressedCommandParameter(view, parameter);
    }

    public static void SetLongPressedCommand(BindableObject view, ICommand value)
    {
        view.SetValue(LongPressedCommandProperty, value);
    }

    public static readonly BindableProperty LongPressedCommandParameterProperty = 
        BindableProperty.CreateAttached("LongPressedCommandParameter", typeof(object), typeof(LongPressedEffect), null);

    public static object GetLongPressedCommandParameter(BindableObject view)
    {
        return view.GetValue(LongPressedCommandParameterProperty);
    }

    public static void SetLongPressedCommandParameter(BindableObject view, object value)
    {
        view.SetValue(LongPressedCommandParameterProperty, value);
    }

    #endregion

    #region LongPressed

    public static readonly BindableProperty ClickCommandProperty = BindableProperty.CreateAttached("ClickCommand", typeof(ICommand),
        typeof(LongPressedEffect), null);


    public static ICommand GetClickCommand(BindableObject view)
    {
        return (ICommand)view.GetValue(ClickCommandProperty);
    }

    public static void SetClickCommand(BindableObject view, ICommand value, object parameter)
    {
        view.SetValue(ClickCommandProperty, value);
        SetClickCommandParameter(view, parameter);
    }

    public static void SetClickCommand(BindableObject view, ICommand value)
    {
        view.SetValue(ClickCommandProperty, value);
    }

    public static readonly BindableProperty ClickCommandParameterProperty =
        BindableProperty.CreateAttached("ClickCommandParameter", typeof(object), typeof(LongPressedEffect), null);

    public static object GetClickCommandParameter(BindableObject view)
    {
        return view.GetValue(ClickCommandParameterProperty);
    }

    public static void SetClickCommandParameter(BindableObject view, object value)
    {
        view.SetValue(ClickCommandParameterProperty, value);
    }

    #endregion
}
}

UWP class:

[assembly: ResolutionGroupName("MyNamespace")]
[assembly: ExportEffect(typeof(MyNamespace.UWP.Effects.UWPLongPressedEffect), "LongPressedEffect")]
namespace MyNamespace.UWP.Effects
{

    public class UWPLongPressedEffect : PlatformEffect
    {
        private bool _attached;

        public UWPLongPressedEffect()
        {

        }

        /// <summary>
        /// Прикрепление обработчика, необходимого для эффекта
        /// </summary>
        protected override void OnAttached()
        {
            if (!_attached)
            {
                if (Control != null)
                {
                    Control.IsHoldingEnabled = true;
                    Control.Tapped += Tapped;
                    Control.Holding += Holding;
                }
                else
                {
                    Container.IsHoldingEnabled = true;
                    Container.Tapped += Tapped;
                    Container.Holding += Holding;
                }
                _attached = true;
            }
        }

        /// <summary>
        /// Отсоединение обработчика для эффекта
        /// </summary>
        protected override void OnDetached()
        {
            if (_attached)
            {
                if (Control != null)
                {
                    Control.IsHoldingEnabled = true;
                    Control.Tapped -= Tapped;
                    Control.Holding -= Holding;
                }
                else
                {
                    Container.IsHoldingEnabled = true;
                    Container.Tapped -= Tapped;
                    Container.Holding -= Holding;
                }
                _attached = false;
            }
        }

        private void Tapped(object sender, TappedRoutedEventArgs e)
        {
            var command = LongPressedEffect.GetClickCommand(Element);
            command?.Execute(LongPressedEffect.GetClickCommandParameter(Element));
        }

        private void Holding(object sender, HoldingRoutedEventArgs e)
        {
            var command = LongPressedEffect.GetLongPressedCommand(Element);
            command?.Execute(LongPressedEffect.GetLongPressedCommandParameter(Element));
        }
    }
}