doc/articles/features/lightsensor.md
[!TIP] This article covers Uno-specific information for LightSensor. For a full description of the feature and instructions on using it, see Use the light sensor.
Windows.Devices.Sensors.LightSensor class allows measuring the illuminance in LUX.| Feature | Windows | Android | iOS | Wasm | macOS | Skia |
|---|---|---|---|---|---|---|
GetDefault | ✔ | ✔ | ✖ | ✔ | ✖ | ✖ |
ReadingChanged | ✔ | ✔ | ✖ | ✔ | ✖ | ✖ |
ReportInterval | ✔ | ✔ | ✖ | ✖ | ✖ | ✖ |
GetDefault method is available on all targets and will return null on those which do not support LightSensor or devices that do not have such a sensor.ReadingChanged events when you no longer need the readings, so that the sensor is no longer active to avoid unnecessary battery consumption.var lightSensor = LightSensor.GetDefault();
lightSensor.ReadingChanged += LightSensor_ReadingChanged;
private async void LightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{
// If you want to update the UI in some way, ensure the Dispatcher is used,
// as the ReadingChanged event handler does not run on the UI thread.
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
OutputTextBlock.Text = $"Sensor reading is " +
$"IlluminanceInLux = {args.Reading.IlluminanceInLux}, " +
$"timestamp = {args.Reading.Timestamp}";
});
}
lightSensor.ReadingChanged -= LightSensor_ReadingChanged;