在UWP中使用滑动手势

时间:2017-10-26 19:48:00

标签: c# visual-studio uwp swipe gesture

我已经看到,自从最新更新(Windows Fall Creators Update)以来,存在一组Swipe Classes,但是在VS(15.4.1)的当前稳定版本中,没有办法让它工作。我目前正在使用Visual Studio 2017 Enterprise(15.4.1)运行最新的W10更新(1709),并且无法使其正常工作。我试图让下面的例子工作,但没有运气:https://channel9.msdn.com/Events/Windows/Windows-Developer-Day-Fall-Creators-Update/WinDev015#comments

1 个答案:

答案 0 :(得分:2)

我正在对您可以在控件上应用的TextBlock应用滑动。

<强> XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Name="SwipeableTextBlock" 
               HorizontalAlignment="Stretch"
               VerticalAlignment="Stretch"
               TextAlignment="Center" Text="No Swipe"
               FontSize="65" FontWeight="Light"
               ManipulationMode="TranslateX,TranslateInertia,System" 
               ManipulationDelta="SwipeableTextBlock_ManipulationDelta"
               ManipulationCompleted="SwipeableTextBlock_ManipulationCompleted"/>
</Grid>

<强> C#

private bool _isSwiped;

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

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

        if (swipedDistance > 0)
        {
            SwipeableTextBlock.Text = "Right Swiped";
        }
        else
        {
            SwipeableTextBlock.Text = "Left Swiped";
        }
        _isSwiped = true;
    }            
}

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

<强>输出 (适用于PC和手机两者) 也会归功于@JustinXL回答,这是示例repository

Output