UWP 中的长按

Longpress in UWP

我正在将应用程序移植到通用 Windows 平台 (Windows 10)。

Android 有一个 onLongPress event。 UWP 是否有等效项?

我发现有一个 Holding event 我试着用这样的东西:

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

问题是在 Windows 桌面上没有触发事件,当使用鼠标而不是触摸时。

默认情况下,鼠标输入不会产生 Holding 事件,您应该使用 RightTapped 事件来显示上下文菜单,当用户在触摸设备中长按并在鼠标设备中单击鼠标右键时触发。

看看GestureRecognizer.Holding and Detect Simple Touch Gestures,你可以使用下面的代码实现

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();
        }
    }
}