了解如何使用环境光传感器检测照明的变化。
重要的应用程序接口(API)
先决条件
应熟悉可扩展应用程序标记语言(XAML)、Microsoft Visual C# 和事件。
你正在使用的设备或仿真器必须支持环境光传感器。
创建简单的光传感器应用
环境光传感器是多种环境传感器之一,允许应用响应用户环境中的更改。
注释
有关更完整的实现,请参阅 光传感器示例。
说明书
创建一个新项目,从 Visual C# 项目模板中选择 空白应用程序(通用 Windows)。
打开项目的BlankPage.xaml.cs文件,并将现有代码替换为以下内容。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Core; // Required to access the core dispatcher object
using Windows.Devices.Sensors; // Required to access the sensor platform and the ALS
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/p/?linkid=234238
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage : Page
{
private LightSensor _lightsensor; // Our app' s lightsensor object
// This event handler writes the current light-sensor reading to
// the textbox named "txtLUX" on the app' s main page.
private void ReadingChanged(object sender, LightSensorReadingChangedEventArgs e)
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, (s, a) =>
{
LightSensorReading reading = (a.Context as LightSensorReadingChangedEventArgs).Reading;
txtLuxValue.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
});
}
public BlankPage()
{
InitializeComponent();
_lightsensor = LightSensor.GetDefault(); // Get the default light sensor object
// Assign an event handler for the ALS reading-changed event
if (_lightsensor != null)
{
// Establish the report interval for all scenarios
uint minReportInterval = _lightsensor.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_lightsensor.ReportInterval = reportInterval;
// Establish the even thandler
_lightsensor.ReadingChanged += new TypedEventHandler<LightSensor, LightSensorReadingChangedEventArgs>(ReadingChanged);
}
}
}
}
需要用你为项目指定的名称重命名前一个代码片段中的命名空间。 例如,如果你创建了一个名为 LightingCS的项目,你就需要将 namespace App1 替换为 namespace LightingCS。
- 打开文件 MainPage.xaml,并将原始内容替换为以下 XML。
<Page
x:Class="App1.BlankPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="Black">
<TextBlock HorizontalAlignment="Left" Height="44" Margin="52,38,0,0" TextWrapping="Wrap" Text="LUX Reading" VerticalAlignment="Top" Width="150"/>
<TextBlock x:Name="txtLuxValue" HorizontalAlignment="Left" Height="44" Margin="224,38,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="217"/>
</Grid>
</Page>
需要将上一代码段中类名的第一部分替换为应用的命名空间。 例如,如果你创建了一个名为 LightingCS的项目,你就需要将 x:Class="App1.MainPage" 替换为 x:Class="LightingCS.MainPage"。 还应将 xmlns:local="using:App1" 替换为 xmlns:local="using:LightingCS"。
- 按 F5 或选择 调试>启动调试 生成、部署和运行应用。
应用运行后,可以通过更改传感器可用的光线或使用仿真器工具来更改光传感器值。
- 返回到 Visual Studio,按 Shift+F5 或选择“调试”“停止调试”> 来结束应用程序。
说明
前面的示例演示了在应用中集成光传感器输入所需的代码很少。
应用在 BlankPage 方法中与默认传感器建立连接。
_lightsensor = LightSensor.GetDefault(); // Get the default light sensor object
应用在 BlankPage 方法内建立报告间隔。 此代码检索设备支持的最小间隔,并将其与请求的间隔 16 毫秒(大约为 60-Hz 刷新率)进行比较。 如果支持的最小间隔大于请求的间隔,则代码会将该值设置为最小值。 否则,它会将值设置为请求的间隔。
uint minReportInterval = _lightsensor.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_lightsensor.ReportInterval = reportInterval;
新的光传感器数据在 ReadingChanged 方法中被捕获。 每次传感器驱动程序从传感器接收新数据时,它都会使用此事件处理程序将值传递给应用。 应用在以下行中注册此事件处理程序。
_lightsensor.ReadingChanged += new TypedEventHandler<LightSensor,
LightSensorReadingChangedEventArgs>(ReadingChanged);
这些新值将被写入到项目的 XAML 文件中的 TextBlock。
<TextBlock HorizontalAlignment="Left" Height="44" Margin="52,38,0,0" TextWrapping="Wrap" Text="LUX Reading" VerticalAlignment="Top" Width="150"/>
<TextBlock x:Name="txtLuxValue" HorizontalAlignment="Left" Height="44" Margin="224,38,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="217"/>