Skip to content

Shake Gestures in WP7

April 11, 2011

Here is a quick and easy step to using the Shake Gesture Library in Windows Phone 7 which I had discovered at the first WP7 workshop in Birmingham, this is essentially an accelerometer built in the phone that identifies a shake movement, and triggers an event handler to do something with that action.

The library detects directions in the left-right (X direction), top-bottom (Y direction), and forward-backward (Z direction).

To use the Shake Gesture Library, you need to: –

1. Add reference to shake gestures library: ShakeGestures.dll.

2. Add a using statement to file header:

using ShakeGestures;

3. Register to the ShakeGesture event:

// register shake event
ShakeGesturesHelper.Instance.ShakeGesture += new    
    EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture);
 
// optional, set parameters
ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 4;

4. implement the ShakeGesture event handler from step 3, this essentially will call this method once a shake has been detected, you can customise the code within the BeginInvoke method.

private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
    _lastUpdateTime.Dispatcher.BeginInvoke(
        () =>
        {
            _lastUpdateTime.Text = DateTime.Now.ToString();
            CurrentShakeType = e.ShakeType;
        });
}

The ShakeGestureEventArgs holds a ShakeType property that identifies the direction of the shake gesture. This ShakeType can be one of three, X, Y or Z direction as described above.

5. Finally, activate the shake gesture helper, which binds to the phone’s accelerometer and starts listening to incoming sensor input events.

// start shake detectionShakeGesturesHelper.Instance.Active = true;
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: