I have a page in my app where I need to take Accelerometer readings. So, I have a variable acc of type Accelerometer which is static to the class corresponding to the page and I have the following statements in the constructor of the class
acc = new Accelerometer();
acc.Start();
acc.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(Acc_Reader);
Acc_Reader updates a variable by taking in the accelerometer readings.
I need to be able to navigate to other pages with the accelerometer running in the background. But, when I come back to this page, the variable which is updated by Acc_Reader starts getting updated at about twice the rate at which it should update. It works perfectly fine as long as you don’t navigate to other pages and come back to this page. So, I’m guessing it has something to do with the constructor of this class (because it gets called again) which contains the above code.
How can I fix this?
Disposing the
accvariable before initializing it works. i.e.So, I think this way you clean up the already running instance before you make a new one.