In this short article I will explain how to use Vibration in Windows Phone 7. One can start and stop the vibration using VibrateController class in Microsoft.Devices. Vibrate is very important feature of any device. It can be used when any action is performed on the device like touch of button. Vibration can be used when you receive email or message or any kind of notification. Vibration can also be used in designing Games.
Let's write code:
Step 1: Add two button in MainPage.xaml, one to start the vibration and the second to stop vibration.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button x:Name="startVibrate" Margin="20,0,0,0" Height="80" Click="startVibrate_Clicked" Content="Start Vibrate" /> <Button x:Name="stopVibrate" Margin="20,200,0,0" Height="80" Click="stopVibrate_Clicked" Content="Stop Vibrate" /> </Grid>
Step 2: Add class level VibrateController.
VibrateController vc;
Let's see the VibrateController class in detail
Start starts the vibration of the device. It accepts time TimeSpan object or double value in milliseconds. The value specified in the Start should between 0 and 5 seconds. Value less than 0 or greater than 5 generates ArgumentOutofRangeException.

Stop stops the vibration.
Default is static method used to get the instance of VibrateController object.
Step 3: Add startVibrate_Clicked and stopVibrate_Clicked event to start and stop the vibration respectively.
private void startVibrate_Clicked(object sender, RoutedEventArgs e) { vc.Start(TimeSpan.FromMilliseconds(5000)); }
private void stopVibrate_Clicked(object sender, RoutedEventArgs e) { vc.Stop(); }
The ideal time to vibrate on any touch response would be 100 miliseconds and for the notification like mail and message it would be 300 miliseconds.
While designing gaming application we can use different time duration for vibration like if you are shooting small tank you might like to vibrate for 500 miliseconds(1/2 seconds) and if you are shooting big tank you might like to vibrate for 1000 miliseconds(1 sec).
Note: Vibration can be tested only on the device not on emulator.
This ends the article of vibration in Windows Phone 7.
|