使用光感應器
了解如何使用環境光線感應器來偵測光源的變化。
重要 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);
}
}
}
}
您必須使用您提供項目的名稱,重新命名上一個代碼段中的命名空間。 例如,如果您已建立名為 AccelerometerCS 的專案,則會將 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>
您必須將上一個代碼段中類別名稱的第一個部分取代為應用程式的命名空間。 例如,如果您已建立名為 AccelerometerCS 的專案,則會將 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"/>