MrTouch
MrTouch

Reputation: 654

WP7 TouchPanel Gesture Handling

I got a Problem with the Gesturehandling.

I mostly did it after this Tutorial:

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

This is my Remote.xaml file:

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

And this is 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;
            }
        }
    }

It doesn't matter how i swipe my finger over the Phone it gives me back horizontal. But this should have been the first step ayway. What i actually need are four directions... that means up, down, right and left.

But i cant find this gesturtypes.. so does anybody has an idea?

it should look like this:

            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;
            }

Thanks.

Upvotes: 1

Views: 1352

Answers (1)

Rejinderi
Rejinderi

Reputation: 11844

I use another way of handling gesture.. not the XNA way. you can try the GestureService.GestureListener

for example u like to detect the drag event inside of a rectangle you do this

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

Then in the code behind you can have event handlers for the gestures

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

Update: this tutorial is good: http://windowsphonegeek.com/articles/WP7-GestureService-in-depth--key-concepts-and-API

You might want to check some of the references for what you will be getting for the event params: http://www.mindscapehq.com/Help/PhoneElements/html/2f4dc2f0-f612-6a89-092e-f65c243caded.htm and http://www.mindscapehq.com/Help/PhoneElements/html/96092851-e003-6423-389c-58d16281122b.htm

There is also a drag delta event that you can look into. Hope it helps in any way.. i am not very familiar with the touch panel thing..sorry about that.. you might want to wait for a few more replies

From the drag start and drag end coordinates you can conclude the direction of drag

..I think you are looking into the flick event.. another good tutorial

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

Update AGAIN: The FlickGestureEventArgs contains a Direction property which is of type System.Windows.Controls.Orientation http://msdn.microsoft.com/en-us/library/system.windows.controls.orientation.aspx

from there you can determine the direction of flick

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
        }
    }
}

Good luck and good night..

Update:

I just saw you should not be using switch cases for the GestureType enumeration... they are integer types

for a list of gesture type see

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

Upvotes: 2

Related Questions