071614_1747_Howtodetect4.jpg

How to detect Shake motion of Windows phone in your application

In this post I will explain you how to detect Shake movement of the phone in your Windows phone application.

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.

We will be basically using a Shake gesture library to achieve this task. To add this functionality in your application follow the certain steps given below.

 

1. Create a new project having the name Shakemotion.

 


 

2. Download the following zip file given below. Unzip the file and add the following reference ShakeGestures.dll to the given project.

 
ShakeGestures,zip
 


 


 

If you face any problems while adding the reference file you can refer to the following blog post link given below.

http://www.windowsapptutorials.com/windows-phone/how-to-integrate-a-toolkit-or-third-party-sdk-with-windows-phone-app-using-nuget-package-manager-2/

 

3. Now add the following namespace given below to MainPage.xaml.cs.

 

 

4. Add the following code given below to the constructor of the MainPage.xaml.cs to register a new shake event in the application and activating it.

 

 

The code for ShakeEventGesture event handler is given below which will be fired whenever user shakes the phone 3 or more times.

 

 

As the following event handler is called the text of displayblock given on the MainPage will change to current system time. This will be repeated every time the user shakes his phone 3 or more times.

 


 

Note: For better understanding this event should be tested on a real device not on emulator.

 


 

5. I hope this post will be helpful to you. You can download the full project source file from the link given below.

 
Download full project file Shakemotion.zip
 

Chetan Gulati

Currently am doing B TECH in IT from Delhi Technological University. App development is my passion. Believe in quality rather than quantity.

  • Junior Cardoso

    using Shakemotion.Resources; not found!!!

    • Chetan gulati

      Remove it..and try again..

  • Junior Cardoso

    Thanks! Working!

  • Jason

    Thanks! The downloaded project works!

    but when I tried making a new project ( windows phone 8.1)

    an error occurred.

    “An exception of type ‘System.IO.FileNotFoundException’ occurred in ShakeGestures.DLL but was not handled in user code

    Additional information: Could not load file or assembly

    ‘Microsoft.Devices.Sensors, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e’

    or one of its dependencies. The system cannot find the file specified.”

    How can I solve this problem?

    Thank you

Read more:
How to create your first Windows Phone app
How to change the App name and Tile name of your Windows phone app
Data binding in Windows Phone app – One way binding
Close