UWP中的Longpress

时间:2016-02-23 10:47:33

标签: c# win-universal-app windows-10-universal

我正在将应用程序移植到Univeral Windows Platform(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桌面上不会触发事件。

1 个答案:

答案 0 :(得分:6)

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

看看GestureRecognizer.HoldingDetect 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();
        }
    }
}