windows-phone-8 - 在 WP8 上实现滑动事件

标签 windows-phone-8 swipe

我正在创建一个 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>

现在我使用上一个按钮和下一个按钮来更改浏览器中的内容。我想使用浏览器上的向左滑动/向右滑动来做到这一点。就像用户向左滑动一样,浏览器内容应该与上一页一起加载,向右滑动结果加载下一页。

那么我必须听什么事件才能实现滑动功能

最佳答案

有两种可能性(AFAIK):
您可以使用 XNA TouchPanel(关于它的更多信息,您可以在这里找到: Working with TouchInputDetecting 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 Toolkitthis answer 中所述或 other here .
编辑 - 小备注
只需要注意,因为当你使用 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.");
       }
    }
}

关于windows-phone-8 - 在 WP8 上实现滑动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399514/

相关文章:

xaml - 定期更新 Windows Phone 8 的动态磁贴

c# - 将类库(例如 WP8、Windows 应用商店)转换为可移植类库

jquery-mobile - jQuery Mobile 在我的屏幕周围创建了一个蓝色边框,如何删除?

android - 如何在android listview中实现fling

windows-phone-8 - 使用 c#.net 在 Windows Phone 8 中将 .mp4 文件转换为 .3gp

c# - 如何使用SQLitePCL在C#Windows Phone App中检查插入操作是否成功

android - cordova 中所有平台的通用 www 文件夹和所有文件

javascript - 简化 JavaScript 中的 switch case

android - 如何在 recyclerview 中使用运动布局

javascript - 将短划视为点击