UWP

时间:2017-08-07 15:33:07

标签: c# xaml uwp uwp-xaml swipe-gesture

是否有任何方法可以使用wptoolkit获取与Windows Phone 8相同的滑动手势。 因为wptoolkit nuget包不适用于uwp,所以我无法在UWP上获得类似的滑动手势 在Windows Phone 8中借助WPtoolkit nugetget包 我放了这个

<toolkit:GestureService.GestureListener>
         <toolkit:GestureListener Flick="OnSwipe"/>
</toolkit:GestureService.GestureListener>

在文本块上,所以我可以在textbox1上从左到右或从右到左滑动。 和滑动手势帮我实现这个

private static int i;
private void OnSwipe(object sender, FlickGestureEventArgs e)
    {
        if (e.HorizontalVelocity < 0)
        {
              i++;
              txtBox1.Text = i.ToString();             
        }

        if (e.HorizontalVelocity > 0)
        {
             i--;
             txtBox1.Text = i.ToString();
        }
    }

我在uwp上尝试使用scrollViewer进行Manupulation方法,但它会不断增加值,直到滚动查看器停止

这是代码

    private static int i;
    private Point initialpoint;
    private void scrollview_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
    {
        initialpoint = e.Position;
    }

    private void scrollview_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        if (e.IsInertial)
        {
            Point currentpoint = e.Position;
            if (currentpoint.X - initialpoint.X >= 2)
            {
                i++;
                txtBox1.Text = i.ToString();
            }

            if (currentpoint.Y - initialpoint.Y >= 2)
            {
                i--;
                txtBox1.Text = i.ToString();
            }
        }
    }

实现相同功能的任何其他方式。

1 个答案:

答案 0 :(得分:2)

实际上,在这种情况下,您不需要处理ManipulationStarted而且您不需要initialPoint财产。假设您已将ScrollViewer ManipulationMode定义为以下

ManipulationMode="TranslateX,TranslateInertia,System"

然后,您只需使用e.Cumulative.Translation.X来判断您总共刷了多长时间

private void scrollview_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    if (e.IsInertial)
    {
        var swipedDistance = e.Cumulative.Translation.X;

        if (Math.Abs(swipedDistance) <= 2) return;

        if (swipedDistance > 0)
        {
            i++;
        }
        else
        {
            i--;
        }

        txtBox1.Text = i.ToString();
    }
}

更新

既然我更了解你的问题,我认为你应该对TextBox本身进行手势操控。如果您想要即时反馈,只需订阅ManipulationDelta事件并创建一个标记,每次触摸只运行一次滑动逻辑。

private bool _isSwiped;
private void txtBox1_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    if (e.IsInertial && !_isSwiped)
    {
        var swipedDistance = e.Cumulative.Translation.X;

        if (Math.Abs(swipedDistance) <= 2) return;

        if (swipedDistance > 0)
        {
            i++;
        }
        else
        {
            i--;
        }

        txtBox1.Text = i.ToString();

        _isSwiped = true;
    }
}

private void txtBox1_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
    _isSwiped = false;
}

确保移动所有处理程序并将ManipulationMode设置为TextBox

<TextBox x:Name="txtBox1"
         ManipulationMode="TranslateX,TranslateInertia,System" 
         ManipulationDelta="txtBox1_ManipulationDelta" 
         ManipulationCompleted="txtBox1_ManipulationCompleted" />