2

I'm building a simple app that changes behavior depending on the ambient light. The app is running fine and the lux values are updated on the display. The problem is that if I change the opacity of a ui object I no longer get any sensor updates (or crash depending what I'm trying to do). Is this a problem with the threading since I'm not familiar with WP8.1 and I've spent a lot of time trying to understand this.

Similar example: http://developer.nokia.com/community/wiki/Using_Windows_phone_8.1_light_sensor

This is the way I initialize the sensor:

// Get the ambient light sensor up and running. 500ms interval.
    var sensor = Windows.Devices.Sensors.LightSensor.GetDefault();
    if (sensor == null)
    {
        return;
    }
    sensor.ReportInterval = 500;
    sensor.ReadingChanged += OnLightSensorReadingChanged;

This is how I get the sensor reading updates:

        void OnLightSensorReadingChanged(Windows.Devices.Sensors.LightSensor sender, Windows.Devices.Sensors.LightSensorReadingChangedEventArgs args)
       {
        Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => 
            LuxReading.Text = "Lux: " + args.Reading.IlluminanceInLux.ToString();
            );
       }

I can see that threads are generated and exited everytime the sensor values change.

Anything I do on the UI will freeze the readings

private void Button_Click(object sender, RoutedEventArgs e)
{
    mypicture.Opacity = 0.5;
}

What am I missing? Should I somehow use a dispatcher to change the opacity?

Hardev
  • 10,851
  • 2
  • 17
  • 17

1 Answers1

0

This isn't an actual answer - just confirmation that the code was ok to begin with. It now works. I don't know why it didn't work before but it does now. I also made some changes to the code so here's a working example:

Creating the actual sensor

public sealed partial class MainPage : Page
{
    LightSensor sensor;
...

protected override void OnNavigatedTo(NavigationEventArgs e)
{
     // Get the ambient light sensor up and running. 500ms polling interval.
    sensor = LightSensor.GetDefault();
    sensor.ReportInterval = 500; // optional, make sure it's above minreportinterval
    sensor.ReadingChanged += sensor_ReadingChanged;
    if (sensor == null)
    {
        return;
    }
}

The event (separate thread)

private void sensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{

    Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () => 
    {
       LuxReading.Text = "Lux: " + args.Reading.IlluminanceInLux.ToString();
       UiUpdate();     
    }
    );

}

Not included but when navigating from the application remember to set the ReportInterval to zero if using non default.

Thanks for all the help.

Hardev
  • 10,851
  • 2
  • 17
  • 17
  • Thanks for the information (I've just deleted my answer as it's not longer needed). Just a remark: look out for `OnNavigatedTo` - it's not getting called when your app is resumed. So for example once you (for example) release the sensor in `OnNavigatingFrom` when app is suspended - for example you hit MS button, change app, or other, then when you get back to your app the event is not fired. NOTE that while debuging mode it will work, hence the app is not suspended, test by firing an app outside VS. Or [invoke suspend to test with debugger](http://stackoverflow.com/a/24103734/2681948) – Romasz Sep 04 '14 at 11:28
  • The above comment concerns WP8.1 RT, not Silverlight. – Romasz Sep 04 '14 at 11:42