在WP8

2014-01-28 16 views
4

實現刷卡事件,我創造了顯示在瀏覽器中的一些HTML文件WP8的應用和結構在WP8

<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內容應該加載上一頁,並向右滑動結果加載下一頁。

那麼,什麼是我要聽來實現刷卡功能

回答

3

有兩種可能性(據我所知)事件:
您可以使用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 Toolkit作爲this answerother here描述。

編輯 - 小此言
只有看出來,因爲當你使用XNA,你有時需要(例如的OnNavigatedTo)做:

FrameworkDispatcher.Update(); 

否則你的應用程序有時會崩潰。

希望這會有所幫助。

EDIT 2 - 註釋後的代碼示例

如果您ManipulationEvent首先handeled(例如,通過透視或地圖),我想訂閱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; 
     } 
    } 
} 

下一個編輯(後下一個評論) - 更好地實施和禁用例如向上,向下的手勢:

如果你不想激活了(小左)/下(LITT對),你必須自己描述條件。請注意,有一個很小的機會(如果有的話),用戶只會左右移動他的手指/上/下 - 所以你必須包括一些保證金。有很多解決方案,你必須嘗試和它一起玩,最簡單的解決方案只是比較手勢的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."); 
     } 
    } 
} 
+0

如何在模擬器上產生這種效果? –

+0

@JMat正如我測試過的 - 在模擬器上,當你單擊(不保持)在屏幕上(或你的瀏覽器,我訂閱了MainPage_Manipulation,因此可以在整個屏幕上工作),並從左側快速移動鼠標或rigt。試一試。 – Romasz

+0

我測試過了,但沒有工作。可能是我正在收聽DragCompleted等其他一些事件(但無法找到它的原因)。你可以發佈示例項目/完整頁面代碼的地方,所以我可以運行和測試 –