UWP中的Longpress

hol*_*s83 5 c# win-universal-app windows-10-universal

我正在将应用程序移植到Univeral Windows Platform(Windows 10).

Android有一个onLongPress活动.UWP有同等效力吗?

我发现有一个Holding事件,我尝试使用这样的东西:

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

问题是当使用鼠标而不是触摸时,Windows桌面上不会触发事件.

Hai*_* Ai 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @holmis83 我不同意你“感觉很自然”的观点。我认为在计算机上按住打开菜单更不自然。即使没有上下文菜单,人们现在也知道右键单击。 (2认同)