Accelerometer.ReadingChanged Accelerometer.ReadingChanged Accelerometer.ReadingChanged Accelerometer.ReadingChanged Accelerometer.ReadingChanged Event

Definition

Occurs each time the accelerometer reports a new sensor reading.

public : event TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> ReadingChanged
// Register
event_token ReadingChanged(TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> const& handler) const;

// Revoke with event_token
void ReadingChanged(event_token const& cookie) const;

// Revoke with event_revoker
ReadingChanged_revoker ReadingChanged(auto_revoke_t, TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> const& handler) const;
public event TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> ReadingChanged
Public Event ReadingChanged As TypedEventHandler (Of Accelerometer, AccelerometerReadingChangedEventArgs)
function onReadingChanged(eventArgs){/* Your code */}


accelerometer.addEventListener("readingchanged", onReadingChanged);
accelerometer.removeEventListener("readingchanged", onReadingChanged);

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.

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

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

        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);

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

The following example shows the ReadingChanged event handler.

async private void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        AccelerometerReading reading = e.Reading;
        ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
        ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
        ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
    });
}

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

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

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

The following example shows the ReadingChanged event handler.

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

    document.getElementById("eventOutputX").innerHTML = reading.accelerationX.toFixed(2);
    document.getElementById("eventOutputY").innerHTML = reading.accelerationY.toFixed(2);
    document.getElementById("eventOutputZ").innerHTML = reading.accelerationZ.toFixed(2);
}

See also