Share via


Windows Phone : Using Accelerometer in Emulator

If you are building application which is dependent on Accelerometer, then the Emulator allows you to test that. The below example was demonstrated on MIX11.

Let’s suppose you have image and based on the X or Y axis of your phone image will rotate.

 <Image Height="428" HorizontalAlignment="Left" Margin="106,96,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="233" Source="/PhoneApp3;component/Images/Phone7.png" >
    <Image.Projection>
        <PlaneProjection x:Name="accImg" ></PlaneProjection>
    </Image.Projection>
</Image>

You need to add the assembly Microsoft.Devices.Sensors. After that

 Accelerometer _acc = new Accelerometer();
public AccelrmtrDemo()
{
    InitializeComponent();            
    _acc.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(_acc_ReadingChanged);
    _acc.Start();
}
               

void _acc_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
    Dispatcher.BeginInvoke(() =>
    {
        accImg.RotationY = -90 * e.X;
        accImg.RotationX = -90 * e.Y;
    });
}

Now you need to use the Emulator’s extender as below

image

And after that move the red pointer to have the feeling of actually moving the Windows Phone Device.

Namoskar!!!