Shaking up your WP7 app is easy to do, with the Shake Gesture Library

Today adding shake based gestures to your WP7 app takes a bit of work. There's no built-in means to turn the raw accelerometer into easily subscribed to gestures/events.

That is there wasn't, until the Shake Gesture Library from the App Hub. With this library added Shake Gesture support to your WP7 app is just a reference and event subscription away

Windows Phone Shake Gestures Library from the Windows Phone Developer Blog provides a good bit of information, details, tips and tricks on this library

There are more than a few very successful mobile applications that use the accelerometer as the main user input. Most commonly used in driving or flying simulation games, the accelerometer in mobile devices is a very powerful tool. But there are other applications/games that use the accelerometer. You can imagine applications like magic 8 ball, where you need to shake the phone to get the perfect answer to a very hard question, implementation of a casino-like slot machine, where you need shake the phone to start rolling the dice, or many other similar implementations. The UX aspect is clear, simply shake or move your phone to trigger some functionality.

Windows Phone has very good support for accelerometer, and the API is relatively easy to use. With that said, while the plain vanilla API for using the raw input from the Windows Phone accelerometer is a good way to start, if you want to drive some logic and gestures, like recognizing a shake gesture, you have to work that in yourself.

Well, lucky you; you came across this blog, which means you’ll read about the Windows Phone Shake Gesture Library that uses the accelerometer to detect a “shake gesture” in 3D space (in three different axes – X, Y, and Z).

Using the library is very simple...

Using the Shake Library in Your Application

Once you download the library, you’ll see the ShakeGestures.dll. Then follow these five steps and you are all set:

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:

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.

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;

As with most App Hub Libraries and Recipes we not only get the sample, but the source to the library as well.

VS Page Snap

Here's a snap of the library usage in the sample

Shake Library Code Snip

The library also comes with a 10 page DocX which goes into a good bit of details on the library and how it works;

Shake Library DocX File Page Snap

Here's some links to help if you don't have a WP7 device handy and want to emulate the accelerometer in the emulator;

 

Page thumbnail courtesy of Adam Barr, http://www.proudlyserving.com/c9glego.htm

Follow the Discussion

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.