|
using System;
using Windows.Devices.Sensors;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace SensorsUltrabook
{
public class AmbientLightSensor : SensorBase
{
public static readonly DependencyProperty IlluminanceProperty = DependencyProperty.Register(
"Illuminance",
typeof (float),
typeof (AmbientLightSensor),
new PropertyMetadata(0.0f));
private readonly LightSensor _lightSensor;
public AmbientLightSensor()
{
_lightSensor = LightSensor.GetDefault();
if (_lightSensor != null)
{
_lightSensor.ReadingChanged += LightSensorReadingChanged;
}
}
public float Illuminance
{
get { return (float) GetValue(IlluminanceProperty); }
protected set { SetValue(IlluminanceProperty, value); }
}
private async void LightSensorReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{
await this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
LightSensorReading reading = args.Reading;
Illuminance = reading.IlluminanceInLux;
}));
}
protected override void OnReportIntervalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
_lightSensor.ReportInterval = (uint) ReportInterval;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have more then 5 years of experience in the software development sectors with more then 15 softwares in C#, WPF, SQL SERVER, WCF, LINQ, intel perceptual computing sdk, digital signature, snmp.