Windows desktop applications
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

LightSensor class

Represents an ambient-light sensor.

This sensor returns the ambient-light reading as a LUX value.

Syntax


public ref class LightSensor sealed 

Members

The LightSensor class has these types of members:

Events

The LightSensor class has these events.

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

 

Methods

The LightSensor class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
GetCurrentReading Gets the current ambient-light sensor reading.
GetDefault Returns the default ambient-light sensor.

 

Properties

The LightSensor class has these properties.

PropertyAccess typeDescription

DeviceId

Read-only

Gets the device identifier.

MinimumReportInterval

Read-only

Gets the minimum report interval supported by the sensor.

ReportInterval

Read/write

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

 

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 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 Windows Store 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);
    });
}


Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.Devices.Sensors
Windows::Devices::Sensors [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8 [Windows Store apps, desktop apps]

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.Devices.Sensors
Windows::Devices::Sensors [C++]

Metadata

Windows.winmd

Attributes

[DualApiPartition()]
[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

See also

Object
LightSensor Sample
Light sensor sample (Windows 10)

 

 

Show:
© 2016 Microsoft