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.
If you face any problems while adding the reference file you can refer to the following blog post link given below.
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.