Search code examples
windows-phone-8.1accelerometer

Windows phone 8.1 stop accelerometer


i am developing an application for windows phone 8.1.

i have an if statement:

if(txtX.Text=="  .24"){

 accelerometer.Stop();
 MainPage.Score++;
 this.frame.Navigate(typeof(Final_Score));
 }

to explain the usage of this statement. I have it so the value of the X axis is placed within txtX.Text. Then when the X axis of the accelerometer is equal to 0.24 it increments 1 to score and navigates to another page. BUT the accelerometer is not stopping and is carrying to run on the next page. When i use the statement accelerometer.Stop();this is not recognised and states

'Windows.Devices.Sensors.Accelerometer'does not contain a definition for 'Stop' and no extension 'Stop' accepting a first arguement of type'Windows.Devices.Sensors.Accelerometer' could be found(are you missing a using directive or an assembly reference?)

Can anyone help or provide the function to stop my accelerometer once the value 0.24 is met.

Thank you.


Solution

  • The class you are working with is Windows.Devices.Sensors.Accelerometer, this does not contain a Stop, you are probably looking at the Microsoft.Devices.Sensors.Accelerometer documentation (which does include a Stop method)

    Instead of Stop just unregister from the Event

    if(txtX.Text=="  .24"){
        accelerometer.ReadingChanged -= YourUpdateFunction;
        MainPage.Score++;
        this.frame.Navigate(typeof(Final_Score));
    }
    

    BTW, it is usually a good idea to unregister from all events when navigating away so that you do not duplicate registration to events when returning.