c# - 在 UWP 中使用滑动手势

标签 c# visual-studio uwp swipe gesture

我已经看到,自从最新更新(Windows Fall Creators Update)以来,存在一组滑动类,但在当前稳定版本的 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

最佳答案

我在您可以在您的控件上应用的 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

关于c# - 在 UWP 中使用滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46962632/

相关文章:

php - xDebug 没有在断点处停止

c# - 如何在 Windows 通用应用程序中显示来自网络文件夹或本地驱动器的图像

javascript - 如何在 UWP 应用程序中播放来自 JS 的声音?

c# - 所有 UWP 样本在启动画面卡住

c# - 文件夹中的新文件

c# - 同一属性上的初始化 + 私有(private)集访问器?

C# Moq 如何获取所有方法调用

visual-studio - 如何在我的 libman.json 中使用我的 Azure DevOps NPM 工件提要作为提供者?

c# - System.Drawing 不存在?

c# - 什么是静态字段的生命周期