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?