SimpleOrientationSensor SimpleOrientationSensor SimpleOrientationSensor SimpleOrientationSensor Class

Definition

Represents a simple orientation sensor.

For an example implementation, see the simple orientation sensor sample.

This sensor detects the current quadrant orientation of the specified device as well as its face-up or face-down status.

public : sealed class SimpleOrientationSensor : ISimpleOrientationSensor, ISimpleOrientationSensor2, ISimpleOrientationSensorDeviceId
public sealed class SimpleOrientationSensor : ISimpleOrientationSensor, ISimpleOrientationSensor2, ISimpleOrientationSensorDeviceId
Public NotInheritable Class SimpleOrientationSensor Implements ISimpleOrientationSensor, ISimpleOrientationSensor2, ISimpleOrientationSensorDeviceId
// This class does not provide a public constructor.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

The following example demonstrates how a UWP app built with XAML and C# uses the GetDefault method to establish a connection to a simple orientation sensor. If no simple orientation sensor is found, the method will return a null value.

_sensor = SimpleOrientationSensor.GetDefault();

The following example demonstrates how a UWP app built with XAML registers a OrientationChanged event handler.

private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_sensor != null)
    {
        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _sensor.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;

        // Display the current orientation once while waiting for the next orientation change
        DisplayOrientation(ScenarioOutput_Orientation, _sensor.GetCurrentOrientation());
    }
    else
    {
        rootPage.NotifyUser("No simple orientation sensor found", NotifyType.StatusMessage);
    }
}

The following example contains the XAML code for the event handler which writes the orientation readings to the application's form.

async private void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        DisplayOrientation(ScenarioOutput_Orientation, e.Orientation);
    });
}
private void DisplayOrientation(TextBlock tb, SimpleOrientation orientation)
{
    switch (orientation)
    {
        case SimpleOrientation.NotRotated:
            tb.Text = "Not Rotated";
            break;
        case SimpleOrientation.Rotated90DegreesCounterclockwise:
            tb.Text = "Rotated 90 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Rotated180DegreesCounterclockwise:
            tb.Text = "Rotated 180 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Rotated270DegreesCounterclockwise:
            tb.Text = "Rotated 270 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Faceup:
            tb.Text = "Faceup";
            break;
        case SimpleOrientation.Facedown:
            tb.Text = "Facedown";
            break;
        default:
            tb.Text = "Unknown orientation";
            break;
    }
}

Properties

DeviceId DeviceId DeviceId DeviceId

Gets the device identifier.

public : Platform::String DeviceId { get; }
public string DeviceId { get; }
Public ReadOnly Property DeviceId As string
var string = simpleOrientationSensor.deviceId;
Value
Platform::String string string string

The device identifier.

ReadingTransform ReadingTransform ReadingTransform ReadingTransform

Gets or sets the transformation that needs to be applied to sensor data. Transformations to be applied are tied to the display orientation with which to align the sensor data.

public : DisplayOrientations ReadingTransform { get; set; }
public DisplayOrientations ReadingTransform { get; set; }
Public ReadWrite Property ReadingTransform As DisplayOrientations
var displayOrientations = simpleOrientationSensor.readingTransform;
simpleOrientationSensor.readingTransform = displayOrientations;
Value
DisplayOrientations DisplayOrientations DisplayOrientations DisplayOrientations

A DisplayOrientations -typed value that specifies the display orientation with which to align the sensor data.

Methods

GetCurrentOrientation() GetCurrentOrientation() GetCurrentOrientation() GetCurrentOrientation()

Gets the default simple orientation sensor.

public : SimpleOrientation GetCurrentOrientation()
public SimpleOrientation GetCurrentOrientation()
Public Function GetCurrentOrientation() As SimpleOrientation
var simpleOrientation = simpleOrientationSensor.getCurrentOrientation();
Returns

Remarks

An application may use this method to poll the sensor for the current reading as an alternative to registering a OrientationChanged event handler. This would be the preferred alternative for an application that updates its user interface at a specific frame rate.

The following example demonstrates how a UWP app built for Windows using JavaScript retrieved the current device orientation by using the simple orientation sensor.

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");
    }
}

GetDefault() GetDefault() GetDefault() GetDefault()

Gets the default simple orientation sensor.

public : static SimpleOrientationSensor GetDefault()
public static SimpleOrientationSensor GetDefault()
Public Static Function GetDefault() As SimpleOrientationSensor
var simpleOrientationSensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault();
Returns

The default simple orientation sensor or null if no simple orientation sensors are found.

Remarks

This method only returns values for hardware that has been integrated into the computer by the manufacturer. (The simple orientation-sensor readings are derived from an accelerometer.) A null value will be returned if the specified sensor is not available in the system.

When a system is in Connected Standby, a call to the GetDefault method will return immediately with a null result.

The following example demonstrates how a UWP app built with XAML and C# used this method to establish a connection to the simple orientation sensor.

_sensor = SimpleOrientationSensor.GetDefault();

The following example demonstrates how a UWP app built for Windows using JavaScript used this method to establish a connection to the simple orientation sensor.

sensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault();

Events

OrientationChanged OrientationChanged OrientationChanged OrientationChanged

Occurs each time the simple orientation sensor reports a new sensor reading

public : event TypedEventHandler OrientationChanged<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>
public event TypedEventHandler OrientationChanged<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>
Public Event TypedEventHandler OrientationChanged( Of ( Of SimpleOrientationSensor ), ( Of SimpleOrientationSensorOrientationChangedEventArgs ))
function onOrientationChanged(eventArgs){/* Your code */}


simpleOrientationSensor.addEventListener("orientationChanged", onOrientationChanged);
simpleOrientationSensor.removeEventListener("orientationChanged", onOrientationChanged);

Remarks

The following example demonstrates how a UWP app built with C# and XAML registers its OrientationChanged event handler.

private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_sensor != null)
    {
        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _sensor.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;

        // Display the current orientation once while waiting for the next orientation change
        DisplayOrientation(ScenarioOutput_Orientation, _sensor.GetCurrentOrientation());
    }
    else
    {
        rootPage.NotifyUser("No simple orientation sensor found", NotifyType.StatusMessage);
    }
}

The following example demonstrates how a UWP app built with XAML registers an OrientationChanged event handler.

async private void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        DisplayOrientation(ScenarioOutput_Orientation, e.Orientation);
    });
}
private void DisplayOrientation(TextBlock tb, SimpleOrientation orientation)
{
    switch (orientation)
    {
        case SimpleOrientation.NotRotated:
            tb.Text = "Not Rotated";
            break;
        case SimpleOrientation.Rotated90DegreesCounterclockwise:
            tb.Text = "Rotated 90 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Rotated180DegreesCounterclockwise:
            tb.Text = "Rotated 180 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Rotated270DegreesCounterclockwise:
            tb.Text = "Rotated 270 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Faceup:
            tb.Text = "Faceup";
            break;
        case SimpleOrientation.Facedown:
            tb.Text = "Facedown";
            break;
        default:
            tb.Text = "Unknown orientation";
            break;
    }
}

The following example demonstrates how a UWP app built with JavaScript registers its OrientationChanged event handler.

function enableReadingChangedScenario() {
    if (sensor) {
        document.addEventListener("msvisibilitychange", msVisibilityChangeHandler, false);
        sensor.addEventListener("orientationchanged", onDataChanged);
        document.getElementById("scenario1Open").disabled = true;
        document.getElementById("scenario1Revoke").disabled = false;
    } else {
        WinJS.log && WinJS.log("No simple orientation sensor found", "sample", "error");
    }
}

The following example shows the OrientationChanged event handler.

function onDataChanged(e) {
    switch (e.orientation) {
        case Windows.Devices.Sensors.SimpleOrientation.notRotated:
            document.getElementById("eventOutputOrientation").innerHTML = "Not Rotated";
            break;
        case Windows.Devices.Sensors.SimpleOrientation.rotated90DegreesCounterclockwise:
            document.getElementById("eventOutputOrientation").innerHTML = "Rotated 90";
            break;
        case Windows.Devices.Sensors.SimpleOrientation.rotated180DegreesCounterclockwise:
            document.getElementById("eventOutputOrientation").innerHTML = "Rotated 180";
            break;
        case Windows.Devices.Sensors.SimpleOrientation.rotated270DegreesCounterclockwise:
            document.getElementById("eventOutputOrientation").innerHTML = "Rotated 270";
            break;
        case Windows.Devices.Sensors.SimpleOrientation.faceup:
            document.getElementById("eventOutputOrientation").innerHTML = "Face Up";
            break;
        case Windows.Devices.Sensors.SimpleOrientation.facedown:
            document.getElementById("eventOutputOrientation").innerHTML = "Face Down";
            break;
        default:
            document.getElementById("eventOutputOrientation").innerHTML = "Undefined orientation " + e.orientation;
            break;
    }
}

See Also