環境光センサーを使用して、照明の変化を検出する方法について説明します。
重要な API
[前提条件]
拡張アプリケーション マークアップ言語 (XAML)、Microsoft Visual C#、およびイベントについて理解している必要があります。
使用しているデバイスまたはエミュレーターは、環境光センサーをサポートしている必要があります。
単純な光センサー アプリを作成する
環境光センサーは、アプリがユーザーの環境の変化に対応できるようにする、いくつかの種類の環境センサーの 1 つです。
注
より完全な実装については、 光センサーのサンプルを参照してください。
インストラクション
Visual C# プロジェクト テンプレートから Blank App (ユニバーサル 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"/>