學習如何使用環境光感測器來偵測光線變化。
這個例子建立了一個簡單的應用程式,依賴光感測器作為輸入裝置。 環境光感測器是多種環境感測器之一,讓應用程式能對使用者環境的變化做出反應。
- 重要 API:Windows。裝置。感測器,光感測器
Note
本文重點介紹示範如何使用光感測器的程式碼。 關於光感測器的概述,請參見 感測器:光感測器。
Prerequisites
你應該熟悉光感應器及其用途。 請參見 感測器:光感測器。
你使用的裝置或模擬器必須支援環境光感測器。
範例程式碼
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Controls;
using Windows.Devices.Sensors;
namespace DevicesDemo.Pages
{
public sealed partial class LightSensorPage : Page
{
private LightSensor? lightSensor;
public LightSensorPage()
{
InitializeComponent();
// Get the default light sensor object.
lightSensor = LightSensor.GetDefault();
if (lightSensor != null)
{
// Establish the report interval.
uint minReportInterval = lightSensor.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
lightSensor.ReportInterval = reportInterval;
// Assign an event handler for the reading-changed event.
lightSensor.ReadingChanged += LightSensor_ReadingChanged;
}
else
{
statusBar.Message = "No light sensor was found.";
statusBar.Severity = InfoBarSeverity.Error;
statusBar.IsOpen = true;
}
}
// This event handler writes the current light
// reading to the text block on the XAML page.
private void LightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
LightSensorReading reading = args.Reading;
txtLuxValue.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
});
}
}
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Margin="24">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="44"/>
<RowDefinition Height="44"/>
<RowDefinition Height="44"/>
</Grid.RowDefinitions>
<TextBlock Text="LUX Reading:" Style="{StaticResource LabelTextBlockStyle}"/>
<TextBlock x:Name="txtLuxValue" Grid.Column="1" Text="---"/>
</Grid>
<InfoBar x:Name="statusBar" Grid.Row="1"/>
</Grid>
應用程式啟動時,你可以透過遮蓋或掀開光感應器來調整光線值。
前一個範例展示了你需要撰寫的必要程式碼,以便將光感應輸入整合進應用程式中。
連接到感測器
呼叫 GetDefault 方法來建立與預設光感應器的連線。
private LightSensor? lightSensor;
// ...
lightSensor = LightSensor.GetDefault();
你也可以呼叫
若未偵測到光感應器,狀態訊息會更新以通知使用者。
設定報告間隔
報告間隔由頁面的建構子設定。 此程式碼會取得裝置所支援的最小間隔,並與請求的 16 毫秒間隔(約 60 Hz 更新率)比較。 若最小支援區間大於請求區間,程式碼會將該值設為最小值。 否則,它會將值設定為所要求的區間。
uint minReportInterval = lightSensor.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
lightSensor.ReportInterval = reportInterval;
讀取感測器資料
新的光感測器資料會被擷取在 ReadingChanged 事件處理程序中。 每當感測器驅動程式從感測器接收到新資料時,就會利用這個事件將數值傳給你的應用程式。 在此範例中,這些新值會寫入對應頁面的 XAML 文字區塊。
lightSensor.ReadingChanged += LightSensor_ReadingChanged;
// ...
private void LightSensor_ReadingChanged(LightSensor sender, LightSensorReadingChangedEventArgs args)
{
DispatcherQueue?.TryEnqueue(DispatcherQueuePriority.Normal, () =>
{
LightSensorReading reading = args.Reading;
txtLuxValue.Text = String.Format("{0,5:0.00}", reading.IlluminanceInLux);
});
}