7

I'm porting an app to Univeral Windows Platform (Windows 10).

Android has a onLongPress event. Is there an equivalent for UWP?

I found there is a Holding event which I tried to use something like this:

private void Rectangle_Holding(object sender, HoldingRoutedEventArgs e)
{
    if (e.HoldingState == HoldingState.Started)
    {
        Debug.WriteLine("Holding started!");
    }
}

Problem is the event is not triggered on Windows Desktop, when mouse is used instead of touch.

holmis83
  • 15,922
  • 5
  • 82
  • 83

1 Answers1

8

Mouse input doesn't produce Holding events by default, you should use RightTapped event to show the context menu, it is triggered when user long presses in a touch device and right clicks in a mouse device.

Take a look at GestureRecognizer.Holding and Detect Simple Touch Gestures, you can achieve that using the following code

public sealed partial class MainPage : Page
{
    GestureRecognizer gestureRecognizer = new GestureRecognizer();

    public MainPage()
    {
        this.InitializeComponent();
        gestureRecognizer.GestureSettings = Windows.UI.Input.GestureSettings.HoldWithMouse;         
    }

    void gestureRecognizer_Holding(GestureRecognizer sender, HoldingEventArgs args)
    {
        MyTextBlock.Text = "Hello";
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        gestureRecognizer.Holding += gestureRecognizer_Holding;
    }

    private void Grid_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        var ps = e.GetIntermediatePoints(null);
        if (ps != null && ps.Count > 0)
        {
            gestureRecognizer.ProcessDownEvent(ps[0]);
            e.Handled = true;
        }
    }

    private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
        gestureRecognizer.ProcessMoveEvents(e.GetIntermediatePoints(null));
        e.Handled = true;
    }

    private void Grid_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        var ps = e.GetIntermediatePoints(null);
        if (ps != null && ps.Count > 0)
        {
            gestureRecognizer.ProcessUpEvent(ps[0]);
            e.Handled = true;
            gestureRecognizer.CompleteGesture();
        }
    }
}
Community
  • 1
  • 1
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • Thank you for your answer, but it is not to open a context menu, so the right-click doesn't feel so natural. – holmis83 Feb 23 '16 at 13:52
  • I have added [GestureRecognizer.Holding](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.input.gesturerecognizer.holding.aspx), and it will solve your problem. – Haibara Ai Feb 24 '16 at 01:20
  • @holmis83, if it have helped, please consider [accepting the answer](http://stackoverflow.com/help/someone-answers) – Haibara Ai Mar 04 '16 at 05:31
  • Sorry for delay. I accept it although `ProcessDownEvent` raises exception "Failed to start tracking the pointer, because it is already being tracked." during some circumstances. – holmis83 Mar 04 '16 at 16:36
  • @holmis83, thanks, then I would suggest you open another question for the exception if it is still unresolved. – Haibara Ai Mar 05 '16 at 03:31
  • 2
    @holmis83 I disagree with your "feel so natural" view. I think holding to open a menu is more unnatural on a computer. People know the right click these days even without context menus. – visc Dec 21 '17 at 14:50