SimpleOrientationSensor.GetCurrentOrientation 方法

定義

取得預設的簡單方向感應器。

public:
 virtual SimpleOrientation GetCurrentOrientation() = GetCurrentOrientation;
SimpleOrientation GetCurrentOrientation();
public SimpleOrientation GetCurrentOrientation();
function getCurrentOrientation()
Public Function GetCurrentOrientation () As SimpleOrientation

傳回

預設的簡單方向感應器。

備註

應用程式可以使用這個方法來輪詢感應器以取得目前讀數,作為註冊 OrientationChanged 事件處理常式的替代方案。 這是以特定畫面播放速率更新其使用者介面之應用程式的慣用替代方案。

下列範例示範如何使用簡單的方向感應器,使用 JavaScript 擷取目前裝置方向的 Windows 建置 UWP 應用程式。

function invokeGetReadingScenario() {
    if (sensor) {
        var orientation = sensor.getCurrentOrientation();

        switch (orientation) {
            case Windows.Devices.Sensors.SimpleOrientation.notRotated:
                document.getElementById("readingOutputOrientation").innerHTML = "Not Rotated";
                break;
            case Windows.Devices.Sensors.SimpleOrientation.rotated90DegreesCounterclockwise:
                document.getElementById("readingOutputOrientation").innerHTML = "Rotated 90";
                break;
            case Windows.Devices.Sensors.SimpleOrientation.rotated180DegreesCounterclockwise:
                document.getElementById("readingOutputOrientation").innerHTML = "Rotated 180";
                break;
            case Windows.Devices.Sensors.SimpleOrientation.rotated270DegreesCounterclockwise:
                document.getElementById("readingOutputOrientation").innerHTML = "Rotated 270";
                break;
            case Windows.Devices.Sensors.SimpleOrientation.faceup:
                document.getElementById("readingOutputOrientation").innerHTML = "Face Up";
                break;
            case Windows.Devices.Sensors.SimpleOrientation.facedown:
                document.getElementById("readingOutputOrientation").innerHTML = "Face Down";
                break;
            default:
                document.getElementById("readingOutputOrientation").innerHTML = "Undefined orientation " + orientation;
                break;
        }
    } else {
        WinJS.log && WinJS.log("No simple orientation sensor found", "sample", "error");
    }
}

適用於