Microsoft Surface Pro具有陀螺仪和加速度计,Windows 8以及完整的.NET框架。
我发现大多数关于动作API的文章指向Windows Phone 8 API。
我应该使用哪些.NET Framework命名空间和类来获取陀螺仪和加速度计数据?
答案 0 :(得分:4)
我刚刚根据文档进行了工作 - 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应用商店应用 - 如果您尝试将其作为控制台/ Windows窗体应用运行,则需要通过以下方式更改目标平台:
答案 1 :(得分:3)
对于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);
}