Udostępnij za pośrednictwem


LightSensor Class

Definition

Represents an ambient-light sensor that provides the ambient-light reading as a LUX value.

public ref class LightSensor sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class LightSensor final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class LightSensor
Public NotInheritable Class LightSensor
Inheritance
Object Platform::Object IInspectable LightSensor
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

For an example implementation, see the light sensor sample.

Remarks

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

_sensor = LightSensor.GetDefault();

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

private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_sensor != null)
    {
        // Establish the report interval
        _sensor.ReportInterval = _desiredReportInterval;

        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _sensor.ReadingChanged += new TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>(ReadingChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;
    }
    else
    {
        rootPage.NotifyUser("No light sensor found", NotifyType.StatusMessage);
    }
}

The following example shows the ReadingChanged event handler.

async private void ReadingChanged(object sender, LightSensorReadingChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        LightSensorReading reading = e.Reading;
        ScenarioOutput_LUX.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
    });
}

Version history

Windows version SDK version Value added
1709 16299 FromIdAsync
1709 16299 GetDeviceSelector
1709 16299 MaxBatchSize
1709 16299 ReportLatency
2004 19041 ReportThreshold

Properties

DeviceId

Gets the device identifier.

MaxBatchSize

Gets the maximum number of events that can be batched by the sensor.

MinimumReportInterval

Gets the minimum report interval supported by the sensor.

ReportInterval

Gets or sets the current report interval for the ambient light sensor.

ReportLatency

Gets or sets the delay between batches of sensor information.

ReportThreshold

Gets the LightSensorDataThreshold for the light sensor.

Methods

FromIdAsync(String)

Asynchronously obtains the sensor from its identifier.

GetCurrentReading()

Gets the current ambient-light sensor reading.

GetDefault()

Returns the default ambient-light sensor.

GetDeviceSelector()

Gets the device selector.

Events

ReadingChanged

Occurs each time the ambient-light sensor reports a new sensor reading.

Applies to

See also