检测Windows 8设备中的移动或摇晃

时间:2012-08-31 18:24:16

标签: windows-8 windows-store-apps accelerometer tablet

在Windows 8 Metro中 - 鉴于它打算在平板电脑上使用,是否可以检测设备是否已被摇动或移动?

This文章似乎无法涵盖设备移动。

如果可以,那么是否有可用的在线教程或代码片段?

1 个答案:

答案 0 :(得分:3)

我认为你正在寻找acceleromatorExample

using Windows.UI.Core;
using Windows.Devices.Sensors;

namespace AccelerometerCS
{

    partial class BlankPage
    {
        // Sensor and dispatcher variables
        private Accelerometer _accelerometer;

        // This event handler writes the current accelerometer reading to 
        // the three acceleration text blocks on the app's main page.

        private void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
        {
            Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
            {
                AccelerometerReading reading = (a.Context as AccelerometerReadingChangedEventArgs).Reading;
                txtXAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
                txtYAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
                txtZAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);

            }, this, e);
        }

        public BlankPage()
        {
            InitializeComponent();
            _accelerometer = Accelerometer.GetDefault();

            if (_accelerometer != null)
            {
                // Establish the report interval
                uint minReportInterval = _accelerometer.MinimumReportInterval;
                uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
                _accelerometer.ReportInterval = reportInterval;

                // Assign an event handler for the reading-changed event
                _accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);
            }

        }
    }
}