OrientationSensor.GetCurrentReading | getCurrentReading Method

获取当前的传感器读数.

语法


var orientationSensorReading = orientationSensor.getCurrentReading();

参数

此 方法 没有参数。

返回值

类型: OrientationSensorReading

当前罗盘读数。

备注

应用程序可以使用此方法轮询当前读取的传感器,以作为注册 ReadingChanged 事件处理程序的替代方法。这是以特定帧速率更新其用户界面的应用程序的首选备用。 是否轮询一次或多次,该应用程序必须建立所需的 ReportInterval。会通知应分配资源满足后续轮询请求的传感器驱动程序

在使用此方法返回值之前,应用程序必须首先检查该值不为 null。(如果该值为 null,而且您尝试检索它,Windows 将生成异常。)

下面的示例演示使用 XAML and C# 生成的 Windows 应用商店应用如何检索方向传感器当前的读数。


private void DisplayCurrentReading(object sender, object args)
{
    OrientationSensorReading reading = _sensor.GetCurrentReading();
    if (reading != null)
    {
        // Quaternion values
        SensorQuaternion quaternion = reading.Quaternion;   // get a reference to the object to avoid re-creating it for each access
        ScenarioOutput_X.Text = String.Format("{0,8:0.00000}", quaternion.X);
        ScenarioOutput_Y.Text = String.Format("{0,8:0.00000}", quaternion.Y);
        ScenarioOutput_Z.Text = String.Format("{0,8:0.00000}", quaternion.Z);
        ScenarioOutput_W.Text = String.Format("{0,8:0.00000}", quaternion.W);

        // Rotation Matrix values
        SensorRotationMatrix rotationMatrix = reading.RotationMatrix;
        ScenarioOutput_M11.Text = String.Format("{0,8:0.00000}", rotationMatrix.M11);
        ScenarioOutput_M12.Text = String.Format("{0,8:0.00000}", rotationMatrix.M12);
        ScenarioOutput_M13.Text = String.Format("{0,8:0.00000}", rotationMatrix.M13);
        ScenarioOutput_M21.Text = String.Format("{0,8:0.00000}", rotationMatrix.M21);
        ScenarioOutput_M22.Text = String.Format("{0,8:0.00000}", rotationMatrix.M22);
        ScenarioOutput_M23.Text = String.Format("{0,8:0.00000}", rotationMatrix.M23);
        ScenarioOutput_M31.Text = String.Format("{0,8:0.00000}", rotationMatrix.M31);
        ScenarioOutput_M32.Text = String.Format("{0,8:0.00000}", rotationMatrix.M32);
        ScenarioOutput_M33.Text = String.Format("{0,8:0.00000}", rotationMatrix.M33);
    }
}


下面的示例演示适用于 Windows 并使用 JavaScript 构建的 Windows 应用商店应用如何检索方向传感器当前的读数。


function getCurrentReading() {
    var reading = sensor.getCurrentReading();
    if (reading) {
        // Quaternion values
        var quaternion = reading.quaternion;    // get a reference to the object to avoid re-creating it for each access
        document.getElementById("readingOutputQuaternion").innerHTML =
               "W: " + quaternion.w.toFixed(6)
            + " X: " + quaternion.x.toFixed(6)
            + " Y: " + quaternion.y.toFixed(6)
            + " Z: " + quaternion.z.toFixed(6);

        // Rotation Matrix values
        var rotationMatrix = reading.rotationMatrix;
        document.getElementById("readingOutputRotationMatrix").innerHTML =
               "M11: " + rotationMatrix.m11.toFixed(6)
            + " M12: " + rotationMatrix.m12.toFixed(6)
            + " M13: " + rotationMatrix.m13.toFixed(6)
            + " M21: " + rotationMatrix.m21.toFixed(6)
            + " M22: " + rotationMatrix.m22.toFixed(6)
            + " M23: " + rotationMatrix.m23.toFixed(6)
            + " M31: " + rotationMatrix.m31.toFixed(6)
            + " M32: " + rotationMatrix.m32.toFixed(6)
            + " M33: " + rotationMatrix.m33.toFixed(6);
    }
}


要求

最低支持客户端

Windows 8

最低支持服务器

不支持“无”

最低支持电话

Windows Phone 8

命名空间

Windows.Devices.Sensors
Windows::Devices::Sensors [C++]

元数据

Windows.winmd

另请参见

OrientationSensor

 

 

显示:
© 2014 Microsoft