有两种可能性(据我所知)事件:
您可以使用XNA的TouchPanel(有关它的更多信息,你可以在这里找到:Working with TouchInput,Detecting Gestures和这blog)本:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Flick;
myWebbrowser.ManipulationCompleted += myWebbrowser_ManipulationCompleted;
}
private void myWebbrowser_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
switch (gesture.GestureType)
{
case GestureType.Flick:
if (e.FinalVelocities.LinearVelocity.X < 0)
LoadNextPage();
if (e.FinalVelocities.LinearVelocity.X > 0)
LoadPreviousPage();
break;
default:
break;
}
}
}
或者你可以使用Silverlight Toolkit作为this answer或other here描述。
编辑 - 小此言
只有看出来,因为当你使用XNA,你有时需要(例如的OnNavigatedTo)做:
FrameworkDispatcher.Update();
否则你的应用程序有时会崩溃。
希望这会有所帮助。
EDIT 2 - 注释后的代码示例
如果您ManipulationEvent首先handeled(例如,通过透视或地图),我想订阅Touch.FrameReported
- 因为我已经测试 - 它应该工作:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Flick;
Touch.FrameReported += Touch_FrameReported;
}
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
if (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
switch (gesture.GestureType)
{
case GestureType.Flick:
if (gesture.Delta.X > 0)
MessageBox.Show("Right");
if (gesture.Delta.X < 0)
MessageBox.Show("Left");
break;
default:
break;
}
}
}
下一个编辑(后下一个评论) - 更好地实施和禁用例如向上,向下的手势:
如果你不想激活了(小左)/下(LITT对),你必须自己描述条件。请注意,有一个很小的机会(如果有的话),用户只会左右移动他的手指/上/下 - 所以你必须包括一些保证金。有很多解决方案,你必须尝试和它一起玩,最简单的解决方案只是比较手势的deltaX和deltaY:
public MainPage()
{
InitializeComponent();
TouchPanel.EnabledGestures = GestureType.Flick | GestureType.HorizontalDrag;
Touch.FrameReported += Touch_FrameReported;
}
TouchPoint firstPoint;
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint mainTouch = e.GetPrimaryTouchPoint(ContentPanel);
if (mainTouch.Action == TouchAction.Down) firstPoint = mainTouch;
else if (mainTouch.Action == TouchAction.Up && TouchPanel.IsGestureAvailable)
{
double deltaX = mainTouch.Position.X - firstPoint.Position.X;
double deltaY = mainTouch.Position.Y - firstPoint.Position.Y;
if (Math.Abs(deltaX) > 2 * Math.Abs(deltaY))
{
if (deltaX < 0) MessageBox.Show("Right.");
if (deltaX > 0) MessageBox.Show("Left.");
}
}
}
如何在模拟器上产生这种效果? –
@JMat正如我测试过的 - 在模拟器上,当你单击(不保持)在屏幕上(或你的浏览器,我订阅了MainPage_Manipulation,因此可以在整个屏幕上工作),并从左侧快速移动鼠标或rigt。试一试。 – Romasz
我测试过了,但没有工作。可能是我正在收听DragCompleted等其他一些事件(但无法找到它的原因)。你可以发布示例项目/完整页面代码的地方,所以我可以运行和测试 –