簡體   English   中英

在WP8上實現Swipe事件

[英]Implement Swipe event on WP8

我正在創建一個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(您可以在此處找到有關它的更多信息: 使用TouchInput檢測手勢和此博客 ):

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM