加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-8 – 在WP8上实现Swipe事件

发布时间:2020-12-14 05:24:38 所属栏目:Windows 来源:网络整理
导读:我正在创建一个wp8应用程序,用于在浏览器中显示一些html文件,结构是 Grid Grid.RowDefinitions RowDefinition Height="Auto"/ RowDefinition Height="*"/ /Grid.RowDefinitions Grid Name="PageNavigationMenu" Grid.ColumnDefinitions ColumnDefinition Wid
我正在创建一个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):

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读