微软Surface Pro有陀螺仪和加速度计、Windows 8和完整的。net框架。
我发现大多数关于运动API的文章都指向了Windows Phone 8 API。
我应该使用什么。net框架名称空间和类来获取陀螺仪和加速度计数据?
我只是根据文档工作- http://msdn.microsoft.com/en-us/library/ie/windows.devices.sensors
using Windows.Devices.Sensors;
private Accelerometer _accelerometer;
private void DoStuffWithAccel()
{
_accelerometer = Accelerometer.GetDefault();
if (_accelerometer != null)
{
AccelerometerReading reading = _accelerometer.GetCurrentReading();
if (reading != null)
double xreading = reading.AccelerationX;
... etc.
}
}
还没有测试过,但它应该适用于任何Windows Store应用程序-如果你想让它作为控制台/Windows窗体应用程序运行,你需要更改targetplatform:
- 右键单击项目->卸载项目
- 按照下面的内容https://software.intel.com/en-us/articles/using-winrt-apis-from-desktop-applications
对于surface pro,您需要使用Windows 8.1
库,而不是Windows Phone 8.1
库。
应该在同一个Windows.Devices.Sensors
命名空间
using Windows.Devices.Sensors;
...
//if you aren't already doing so, and you want the default sensor
private void Init()
{
_accelerometer = Accelerometer.GetDefault();
_gyrometer = Gyrometer.GetDefault();
}
...
private void DisplayAccelReading(object sender, object args)
{
AccelerometerReading reading = _accelerometer.GetCurrentReading();
if (reading == null)
return;
ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
}
...
private void DisplayGyroReading(object sender, object args)
{
GyrometerReading reading = _gyrometer.GetCurrentReading();
if (reading == null)
return;
ScenarioOutput_AngVelX.Text =
String.Format("{0,5:0.00}", reading.AngularVelocityX);
ScenarioOutput_AngVelY.Text =
String.Format("{0,5:0.00}", reading.AngularVelocityY);
ScenarioOutput_AngVelZ.Text =
String.Format("{0,5:0.00}", reading.AngularVelocityZ);
}