【发布时间】:2015-02-09 15:40:55
【问题描述】:
如何确定用户是在执行左还是右swipe?我找到了一个教程,向我展示了如何使用 CrossSlide 事件。我从教程中得到的代码:
Mainpage.xaml:
<Grid Name="LayoutRoot">
<TextBox Name="TxtGestureNotes"
Text="Hallo"
VerticalAlignment="Center"
HorizontalAlignment="Center">
</TextBox>
</Grid>
Mainpage.xaml.cs:
public sealed partial class MainPage : Page
{
GestureRecognizer gestureRecognizer = new GestureRecognizer();
Windows.UI.Xaml.UIElement element;
public MainPage()
{
this.InitializeComponent();
Page_Loaded();
}
public void GestureInputProcessor(Windows.UI.Input.GestureRecognizer gr, Windows.UI.Xaml.UIElement target) {
this.gestureRecognizer = gr;
//Targeted UI element to be performing gestures on it.
this.element = target;
//Enable gesture settings for Tap, CrossSlide
this.gestureRecognizer.GestureSettings = GestureSettings.Tap | GestureSettings.CrossSlide;
// set up pointer event handlers. these receive input events that are used by the gesture recognizer
this.element.PointerCanceled += OnPointerCanceled;
this.element.PointerPressed += OnPointerPressed;
this.element.PointerReleased += OnPointerReleased;
this.element.PointerMoved += OnPointerMoved;
// set up event handlers to respond to gesture recognizer
gestureRecognizer.Tapped += gestureRecognizer_Tapped;
//CrossSliding distance thresholds are disabled by default. Use CrossSlideThresholds to set these values.
Windows.UI.Input.CrossSlideThresholds cst = new Windows.UI.Input.CrossSlideThresholds();
cst.SelectionStart = 2;
cst.SpeedBumpStart = 3;
cst.SpeedBumpEnd = 4;
cst.RearrangeStart = 5;
gestureRecognizer.CrossSlideHorizontally = true;
gestureRecognizer.CrossSlideThresholds = cst;
gestureRecognizer.CrossSliding += gestureRecognizer_CrossSliding;
}
private void Page_Loaded() {
// For makig gestures operations on Grid named as 'LayoutRoot'
GestureInputProcessor(gestureRecognizer, LayoutRoot);
}
private void Page_Unloaded(object sender, RoutedEventArgs e) {
// Remove event handlers of gesture recognizers events
gestureRecognizer.Tapped -= gestureRecognizer_Tapped;
gestureRecognizer.CrossSliding -= gestureRecognizer_CrossSliding;
}
// Pointer Events
private void OnPointerCanceled(object sender, PointerRoutedEventArgs e) {
this.gestureRecognizer.CompleteGesture();
e.Handled = true;
}
private void OnPointerPressed(object sender, PointerRoutedEventArgs e) {
// Route the events to the gesture recognizer
this.gestureRecognizer.ProcessDownEvent(e.GetCurrentPoint(this.element));
//Set the pointer capture to the element being interacterd with
this.element.CapturePointer(e.Pointer);
// Mark the event handled to prevent execution of default handlers
e.Handled = true;
}
private void OnPointerReleased(object sender, PointerRoutedEventArgs e) {
this.gestureRecognizer.ProcessUpEvent(e.GetCurrentPoint(this.element));
e.Handled = true;
}
private void OnPointerMoved(object sender, PointerRoutedEventArgs e) {
this.gestureRecognizer.ProcessMoveEvents(e.GetIntermediatePoints(this.element));
}
private void gestureRecognizer_Tapped(GestureRecognizer sender, TappedEventArgs args) {
TxtGestureNotes.Text = "Tap gesture recognized";
}
private void gestureRecognizer_CrossSliding(GestureRecognizer sender, CrossSlidingEventArgs args) {
TxtGestureNotes.Text = "Slide gesture recognized";
}
}
但是如何区分左右滑动手势?任何帮助将不胜感激。
【问题讨论】:
-
那么你从你提供的代码中得到了什么?
-
我得到一个可以左右滑动的应用程序,它正在显示“已识别滑动手势”。但我想要一个可以区分左右滑动的应用程序。就像显示“左滑识别”/“右滑识别”一样。 @Kulasangar
标签: c# xaml windows-phone-8 gesture-recognition