如何确定用户是使用CrossSlide(手势识别器)向左还是向右滑动?(WP8)
本文关键字:WP8 用户 何确定 手势识别 CrossSlide | 更新日期: 2024-04-03 08:27:48
如何确定用户是在执行左swipe
还是右?我找到了一个教程,告诉我如何使用CrossSlide Event。我从教程中得到的代码:
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";
}
}
但是如何区分滑动手势,例如向右或向左?任何帮助将不胜感激。
尝试使用 Silverlight Toolkit 中的GestureService
来识别Swiping Gesture
。
如何在Windows Phone 7中滑动
在上面的示例中,尝试显示一些内容:
if (e.HorizontalVelocity < 0)
{
// Display something
MessageBox.Show("Swiped Right");
}
// User flicked towards right
if (e.HorizontalVelocity > 0)
{
// Display something
MessageBox.Show("Swiped Right");
}