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.ReadingChanged | readingchanged event

Applies to Windows and Windows Phone

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

Syntax


function onReadingChanged(eventArgs) { /* Your code */ }
 
// addEventListener syntax
lightSensor.addEventListener("readingchanged", onReadingChanged);
lightSensor.removeEventListener("readingchanged", onReadingChanged);
 
- or -

lightSensor.onreadingchanged = onReadingChanged;


Event information

Delegate TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>

Remarks

An application may register this event handler to obtain sensor readings. The application must establish a desired ReportInterval. This informs the sensor driver that resources should be allocated to satisfy the requirements of the application.

Applications can set the frequency of this event by setting the ReportInterval property.

The following example demonstrates how a Windows Store app built with C# and XAML registers its 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);
    });
}


The following example demonstrates how a Windows Store appbulit with JavaScript registers its ReadingChanged event handler.


function enableReadingChangedScenario() {
    if (lightSensor) {
        // Set the reportInterval to enable the sensor events
        lightSensor.reportInterval = reportInterval;

        document.addEventListener("msvisibilitychange", msVisibilityChangeHandler, false);
        lightSensor.addEventListener("readingchanged", onDataChanged);
        document.getElementById("scenario1Open").disabled = true;
        document.getElementById("scenario1Revoke").disabled = false;
    } else {
        WinJS.log && WinJS.log("No light sensor found", "sample", "error");
    }
}


The following example shows the ReadingChanged event handler.


function onDataChanged(e) {
    var reading = e.reading;

    document.getElementById("eventOutputIlluminance").innerHTML = e.reading.illuminanceInLux.toFixed(2);
}


Requirements

Minimum supported client

Windows 8

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

See also

LightSensor

 

 

Show:
© 2014 Microsoft