在 Windows 8 Metro 中 - 鉴于它旨在用于平板电脑,是否可以检测设备是否被摇晃或移动?
这篇文章似乎没有涵盖设备移动。
如果这是可能的,那么是否有任何可用的在线教程或代码片段?
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);
}
}
}
}