Search code examples
c#visual-studiouwpswipegesture

Use Swipe Gesture in UWP


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


Solution

  • 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