加载中

In this article I will talk about Windows Phone Shake gesture. Microsoft provides Shake gesture library which is available in AppHub.

Let's see how we can do this:

Step 1: Download Shake gesture library from http://create.msdn.com/en-us/education/catalog/article/Recipe_Shake_Gesture_Library

Step 2: Unzip Shake gestures library.zip and navigate to debug folder of ShakeGestures.

在这篇文章中我们将介绍 Windows Phone 的摇晃功能。让我们来看如何实现。

Step 1: 下载摇晃手势开发库 http://create.msdn.com/en-us/education/catalog/article/Recipe_Shake_Gesture_Library

Step 2: 解压后进入 ShakeGesture 的 debug 文件夹

Windows Phone - Shake gestures library

Step 3: Create a silverlight for Windows Phone project.

Step 4: Add ShakeGestures dll to project.

Step 5: Add a textblock inside contentpanel of MainPage.xaml.

<TextBlock Text="" Height="60" HorizontalAlignment="Left" Margin="100,100,0,0" Name="txtShakeType" VerticalAlignment="Top" FontSize="40" />

Step 3: 创建一个 Sliverlight for Windows Phone 项目

Step 4: 将 ShakeGestures dll 添加到项目中

Windows Phone - ShakeGestures dll

Step 5: 在 MainPage.xaml 中添加 TextBlock

<TextBlock Text="" Height="60" HorizontalAlignment="Left" Margin="100,100,0,0" Name="txtShakeType" VerticalAlignment="Top" FontSize="40" />

Step 6: Add ShakeGestures directive.
using ShakeGestures;

Step 7: Modify constructor of MainPage.xaml.cs as below. MinimumRequiredMovesForShake is set to 2 which means the device have to be shaked twice (One in each direction) on same axis.

public MainPage()
{
   InitializeComponent();
   ShakeGesturesHelper.Instance.ShakeGesture += new EventHandler(Instance_ShakeGesture);
   ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
   ShakeGesturesHelper.Instance.Active = true;
}

Step 8: Instance_ShakeGesture will be invoked on shake of device to update UI.

private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
   this.Dispatcher.BeginInvoke(() =>
   {
      txtShakeType.Text = String.Format("Shaked Axis = {0}", e.ShakeType);
   });
}

Step 9: Now run the application and shake the device on X, Y and Z axis. The device will show shake along different axis like below.

This ends the article of Shake in Windows Phone.

Step 6: 引入 ShakeGestures.

using ShakeGestures;
Step 7: 修改 MainPage.xaml.cs 中的构造器,代码如下。MinimumRequiredMovesForShake 设置为 2 表示需要在同一个方向摇晃两次 
public MainPage()
{
   InitializeComponent();
   ShakeGesturesHelper.Instance.ShakeGesture += new EventHandler(Instance_ShakeGesture);
   ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
   ShakeGesturesHelper.Instance.Active = true;
}

Step 8: Instance_ShakeGesture 用于摇晃时更新 UI 显示:

private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
   this.Dispatcher.BeginInvoke(() =>
   {
      txtShakeType.Text = String.Format("Shaked Axis = {0}", e.ShakeType);
   });
}

Step 9: 现在运行程序,然后使用不同的方向摇晃手机,设备上将会显示不同轴的摇晃信息:

WP-Shake X Axis, Y Axis, Z Axis

返回顶部
顶部