SimpleOrientationSensor SimpleOrientationSensor SimpleOrientationSensor SimpleOrientationSensor Class

Represents a simple orientation sensor.

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

Syntax

Declaration

public sealed class SimpleOrientationSensorpublic sealed class SimpleOrientationSensorPublic NotInheritable Class SimpleOrientationSensor

Remarks

The following example demonstrates how a Windows Store 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 Windows Store 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 summary

Gets the device identifier.

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.

Methods summary

Gets the default simple orientation sensor.

Gets the default simple orientation sensor.

Events summary

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

Properties

  • DeviceId
    DeviceId
    DeviceId
    DeviceId

    Gets the device identifier.

    public string DeviceId { get; }public string DeviceId { get; }Public ReadOnly Property DeviceId As string

    Property Value

    • 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

    Property Value

Methods

  • GetCurrentOrientation()
    GetCurrentOrientation()
    GetCurrentOrientation()
    GetCurrentOrientation()

    Gets the default simple orientation sensor.

    public SimpleOrientation GetCurrentOrientation()public SimpleOrientation GetCurrentOrientation()Public Function GetCurrentOrientation() As SimpleOrientation

    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 Windows Store 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

    Returns

    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 Windows Store 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 Windows Store 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 OrientationChangedpublic event TypedEventHandler OrientationChangedPublic Event OrientationChanged

    Remarks

    The following example demonstrates how a Windows Store 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 Windows Store 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 Windows Store 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;
        }
    }
    

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.DualApiPartitionAttribute
Windows.Foundation.Metadata.ContractVersionAttribute

Details

Assembly

Windows.Devices.Sensors.dll