doc/articles/features/orientation-sensor.md
[!TIP] This article covers Uno-specific information for SimpleOrientationSensor. For a full description of the feature and instructions on using it, see SimpleOrientationSensor Class.
Windows.Devices.Sensors.SimpleOrientationSensor class allows you to determine the general orientation of the device.| Feature | Windows | Android | iOS | Web (WASM) | macOS | Linux (Skia) | Win 7 (Skia) |
|---|---|---|---|---|---|---|---|
GetDefault | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
OrientationChanged | ✔ | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
[!IMPORTANT] The
OrientationChangedevent is not supported by iOS simulators.
GetDefault method is available on all targets and will return null on those that do not support SimpleOrientationSensor or devices that do not have such a sensor.OrientationChanged event when you no longer need the readings so that the sensor is no longer active to avoid unnecessary battery consumption.var simpleOrientationSensor = SimpleOrientationSensor.GetDefault();
simpleOrientationSensor.OrientationChanged += SimpleOrientationSensor_OrientationChanged;
private async void SimpleOrientationSensor_OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs args)
{
// If you want to update the UI in some way, ensure the Dispatcher is used,
// as the OrientationChanged event handler does not run on the UI thread.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
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;
}
});
}
simpleOrientationSensor.OrientationChanged -= SimpleOrientationSensor_OrientationChanged;