在 WP8 上实现滑动事件 [英] Implement Swipe event on WP8

查看:29
本文介绍了在 WP8 上实现滑动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 wp8 应用程序,用于在浏览器中显示一些 html 文件,其结构是

I am creating a wp8 application for showing some html files in a browser and the structure is

    <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>

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

Now i am using a previous button and next button to change the content in browser . I want to do it using the Swipe Left / Swipe Right on browser . Like if user Swipe left direction , borswer content should be loaded with previous page and Swipe right results to load next page.

那么我必须监听哪些事件才能实现滑动功能

So what are the events i have to listen to implement swipe feature

推荐答案

有两种可能性 (AFAIK):
您可以使用 XNA TouchPanel(有关它的更多信息,您可以在此处找到:使用 TouchInput检测手势和这个博客)这:

There are two possibilities (AFAIK):
You can use XNA TouchPanel (more information about it you can find here: Working with TouchInput, Detecting Gestures and this blog) for this:

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,如这个答案其他在这里.

编辑 - 小备注
只能当心了,因为当你使用 XNA 时,你有时需要这样做(例如 OnNavigatedTo):

Or you can use Silverlight Toolkit as described in this answer or other here.

EDIT - small remark
Only watch out, because when you use XNA, you sometimes need to do (for example OnNavigatedTo):

FrameworkDispatcher.Update();

否则您的应用有时会崩溃.

Otherwise your App will sometimes crash.

希望这会有所帮助.

EDIT 2 - 注释后的代码示例

如果您的 ManipulationEvent 首先处理(例如通过 Pivot 或 Map),我尝试订阅 Touch.FrameReported - 正如我测试过的 - 它应该可以工作:

Hope this helps.

EDIT 2 - code example after comment

In case your ManipulationEvent is handeled first (for example by Pivot or Map), I tried to subscribe to Touch.FrameReported - as I've tested - it should work:

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;
      }
   }
}

NEXT EDIT(在下一个评论之后) - 更好的实现和禁用向上、向下手势的示例:

NEXT EDIT (after next comments) - better implementation and example of disabling up, down gestures:

如果你不想激活向上(左小)/向下(小右),你必须自己描述条件.请注意,用户仅向左/向右/向上/向下移动手指的可能性很小(如果有) - 因此您必须包含一些边距.有很多解决方案,您必须尝试使用​​它,最简单的解决方案只是比较手势的 deltaX 和 deltaY:

If you don't want to activate for up (little left)/down (little right), you have to describe conditions yourself. Be aware that there is a very little chance (if any) that user will move his finger only left/right/up/down - so you have to include some margin. There are many solutions for that, you have to try and play with it, the simpliest solutions just compare deltaX and deltaY of gesture:

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.");
       }
    }
}

这篇关于在 WP8 上实现滑动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆