Use Swipe Gesture in UWP

2019-08-26 14:12发布

I've seen that since the latest update (Windows Fall Creators Update) there exists a collection of Swipe Classes, but in the current stable release of VS ( 15.4.1 ) there isn't a way to make it work. I'm currently running the latest W10 update (1709) with Visual Studio 2017 Enterprise ( 15.4.1 ) and there's no way to make it work. I've tried to make the following example work but with no luck: https://channel9.msdn.com/Events/Windows/Windows-Developer-Day-Fall-Creators-Update/WinDev015#comments

1条回答
Animai°情兽
2楼-- · 2019-08-26 15:00

I am applying swipe on a TextBlock you can apply on your control.

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

Output (Works On PC and Mobile Both) also credits to @JustinXL answer and this is sample repository

Output

查看更多
登录 后发表回答