在WP8

2014-01-28 66 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等其他一些事件(但无法找到它的原因)。你可以发布示例项目/完整页面代码的地方,所以我可以运行和测试 –