在UWP中使用滑动手势 [英] Use Swipe Gesture in UWP
问题描述
我已经看到,自从最新更新(Windows Fall Creators Update)以来,存在一个Swipe类的集合,但是在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
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
推荐答案
我正在对可在控件上应用的TextBlock应用滑动.
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;
}
输出 (在PC和Mobile上均可使用) 也归功于 @JustinXL 答案,这是示例
Output (Works On PC and Mobile Both) also credits to @JustinXL answer and this is sample repository
这篇关于在UWP中使用滑动手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!