WP7 TouchPanel手势处理

时间:2012-03-28 14:36:17

标签: silverlight events windows-phone-7 gesture

我遇到了手势问题。

我主要是在本教程之后做的:

http://www.nickharris.net/2010/11/using-touchpanel-for-gestures-in-windows-phone-7/

这是我的Remote.xaml文件:

 <UserControl x:Class="EnergyRadio.Remote"
 ....
 ManipulationCompleted="StackPanel_ManipulationCompleted">
      <Grid x:Name="LayoutRoot" Background="Transparent" >
      </Grid>
</UserControl>

这是Remote.xaml.cs:

 public Remote()
    {
        InitializeComponent();
        TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.HorizontalDrag;
    }

    private void StackPanel_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
    {
        //Check if touch Gesture is available  
        if (TouchPanel.IsGestureAvailable)
        {
            // Read the gesture so that you can handle the gesture type  
            GestureSample gesture = TouchPanel.ReadGesture();
            switch (gesture.GestureType)
            {
                case GestureType.VerticalDrag:
                    MessageBox.Show("vertikal");
                    break;
                case GestureType.HorizontalDrag:
                    MessageBox.Show("horizontal");
                    break;
                default:
                    //do something  
                    break;
            }
        }
    }

无论我如何在手机上滑动手指,它都会让我回到水平位置。但这应该是第一步。我真正需要的是四个方向......这意味着向上,向下,向右和向左。

但是我找不到这种类型的怀孕论者。所以有人有想法吗?

它应该是这样的:

            switch (gesture.GestureType)
            {
                case "GesturType.Up":
                    MessageBox.Show("Volume Up");
                    break;
                case "GesturType.Down":
                    MessageBox.Show("Volume Down");
                    break;
                case "GesturType.Right":
                    MessageBox.Show("Next Channel");
                    break;
                case "GesturType.Left":
                    MessageBox.Show("Previous Channel");
                    break;
                default:
                    //do something  
                    break;
            }

感谢。

1 个答案:

答案 0 :(得分:2)

我使用另一种处理手势的方式..而不是XNA方式。你可以试试GestureService.GestureListener

例如,你喜欢在你做这个的矩形内部检测拖拽事件

   <Rectangle x:Name="HelloRect">
      <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener DragStarted="DragStarted_EventHandler"  DragCompleted="DragCompleted_EventHandler" />
      </toolkit:GestureService.GestureListener>
   </Rectangle>

然后在后面的代码中,您可以拥有手势的事件处理程序

private void DragStarted_EventHandler(object sender, DragStartedGestureEventArgs e)
{
    this.HelloRect.Fill = new SolidColorBrush(Colors.White);
}

private void DragCompleted_EventHandler(object sender, DragCompletedGestureEventArgs e)
{
    this.HelloRect.Fill = new SolidColorBrush(Colors.Black);
}

更新:本教程很好: http://windowsphonegeek.com/articles/WP7-GestureService-in-depth--key-concepts-and-API

您可能想要查看一些参考资料,了解您将为事件参数获取的内容: http://www.mindscapehq.com/Help/PhoneElements/html/2f4dc2f0-f612-6a89-092e-f65c243caded.htmhttp://www.mindscapehq.com/Help/PhoneElements/html/96092851-e003-6423-389c-58d16281122b.htm

您还可以查看拖拽增量事件。 希望它能以任何方式帮助..我对触摸板的东西不是很熟悉......对此有所了解......你可能想再等几个回复

从拖动开始和拖动结束坐标,您可以结束拖动方向

..我认为你正在研究电影事件......另一个很好的教程

http://johnhforrest.com/2010/09/windows-phone-7-gestures/

再次更新: FlickGestureEventArgs包含一个Direction属性,其类型为System.Windows.Controls.Orientation http://msdn.microsoft.com/en-us/library/system.windows.controls.orientation.aspx

从那里你可以确定轻弹的方向

private void Flick_EventHandler(object sender, FlickGestureEventArgs e)
{
    if (e.Direction == Orientation.Horizontal)
    {
        if (e.HorizontalVelocity < 0)
        {
            //Right flick
        }
        else
        {
            //Left flick
        }
    }
    else
    {
        if (e.VerticalVelocity < 0)
        {
            //Up flick
        }
        else
        {
            //Down flick
        }
    }
}
祝你好运,晚安..

更新

我刚看到你不应该为GestureType枚举使用switch case ...它们是整数类型

有关手势类型的列表,请参阅

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.touch.gesturetype.aspx