使用手机重量加速器

 Accelerometer sensor;
        private void ApplicationBarMenuItem_Click_2(object sender, EventArgs e)
        {
            //NavigationService.Navigate(new Uri("/Gyroscope_Test.xaml", UriKind.Relative));
            sensor = new Accelerometer() { TimeBetweenUpdates = TimeSpan.FromSeconds(1) };
            sensor.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(sensor_CurrentValueChanged);
            if (sensor.State == SensorState.NotSupported)
            {
                MessageBox.Show("本手机不支持加速计。");
                return;
            }
            try
            {
                sensor.Start();
            }
            catch (AccelerometerFailedException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        void sensor_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            Dispatcher.BeginInvoke(delegate
            {
                if (e.SensorReading.Acceleration.X > 0.4)
                {
                    if (pivot.SelectedIndex == 4)
                    {
                        pivot.SelectedIndex = 0;
                    }
                    else
                    {
                        pivot.SelectedIndex = pivot.SelectedIndex + 1;
                    }
                }

                if (e.SensorReading.Acceleration.X < -0.4)
                {
                    if (pivot.SelectedIndex == 0)
                    {
                        pivot.SelectedIndex = 4;
                    }
                    else
                    {
                        pivot.SelectedIndex = pivot.SelectedIndex - 1;
                    }
                }

                pivot.Title = string.Format("[{0:0.00},{1:0.00},{2:0.00}] at {3}", e.SensorReading.Acceleration.X, e.SensorReading.Acceleration.Y, e.SensorReading.Acceleration.Z, e.SensorReading.Timestamp.TimeOfDay.ToString());

            });
        }

 

你可能感兴趣的:(使用)