我正在创建一个wp8应用程序,用于在浏览器中显示一些html文件,结构是
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid Name="PageNavigationMenu"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Button Height="70" Content="P" Grid.Column="0" VerticalContentAlignment="Center" Click="btnPrevious_Click" x:Name="btnPrevious" ></Button> <Button Height="70" Content="N" Grid.Column="1" VerticalAlignment="Center" Click="btnNext_Click" x:Name="btnNext"></Button> </Grid> <Grid Grid.Row="1" Hold="Grid_Hold"> <phone:WebBrowser IsScriptEnabled="True" x:Name="mainBrowserControl"> </phone:WebBrowser> </Grid> </Grid>
现在我使用上一个按钮和下一个按钮来更改浏览器中的内容.我想在浏览器上使用向左滑动/向右滑动来执行此操作.如果用户向左滑动方向,则应使用上一页加载borswer内容,然后向右滑动结果以加载下一页.
那么我必须听取哪些事件来实现滑动功能
解决方法
有两种可能性(AFAIK):
你可以使用XNA TouchPanel(有关它的更多信息,你可以在这里找到: Working with TouchInput,Detecting Gestures和这个 blog):
你可以使用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; } } }
或者您可以使用this answer或other here中所述的Silverlight Toolkit.
编辑 – 小言
只留意,因为当你使用XNA时,你有时需要这样做(例如OnNavigatedTo):
FrameworkDispatcher.Update();
否则你的应用程序有时会崩溃.
希望这可以帮助.
编辑2 – 评论后的代码示例
如果您的ManipulationEvent首先被处理(例如通过Pivot或Map),我尝试订阅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; } } }
下一个编辑(在下一个评论之后) – 更好的实现和禁用向上,向下手势的示例:
如果你不想激活(向左)/向下(向右),你必须自己描述条件.请注意,用户只会向左/向右/向上/向下移动手指的可能性很小(如果有的话) – 因此您必须包含一些余量.有很多解决方案,你必须尝试使用它,最简单的解决方案只是比较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."); } } }