OrientationSensor 類別

定義

表示方向感應器。

此感應器會傳迴旋轉矩陣和四元數,可用來調整遊戲應用程式中使用者的觀點。

如需範例實作,請參閱方向感應器範例

public ref class OrientationSensor sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class OrientationSensor final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class OrientationSensor
Public NotInheritable Class OrientationSensor
繼承
Object Platform::Object IInspectable OrientationSensor
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

備註

感應器資料會相對於裝置的固定感應器座標系統提供,而且與顯示方向無關。 對於依賴感應器資料進行輸入控制項或操作螢幕上元素的應用程式,開發人員必須將目前的顯示方向納入考慮,並適當地補償資料。 如需感應器座標系統的詳細資訊,請參閱 感應器資料和顯示方向

下列範例示範使用 XAML 和 C# 建置的 UWP 應用程式如何使用 GetDefault 方法來建立方向感應器的連線。 如果找不到方向感應器,方法會傳回 Null 值。

_sensor = OrientationSensor.GetDefault();

下列範例示範如何使用 XAML 建置的 UWP 應用程式註冊 ReadingChanged 事件處理常式。

private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_sensor != null)
    {
        // Establish the report interval
        _sensor.ReportInterval = _desiredReportInterval;

        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _sensor.ReadingChanged += new TypedEventHandler<OrientationSensor, OrientationSensorReadingChangedEventArgs>(ReadingChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;
    }
    else
    {
        rootPage.NotifyUser("No orientation sensor found", NotifyType.StatusMessage);
    }
}

下列範例顯示 ReadingChanged 事件處理常式。

async private void ReadingChanged(object sender, OrientationSensorReadingChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        OrientationSensorReading reading = e.Reading;

        // Quaternion values
        SensorQuaternion quaternion = reading.Quaternion;   // get a reference to the object to avoid re-creating it for each access
        ScenarioOutput_X.Text = String.Format("{0,8:0.00000}", quaternion.X);
        ScenarioOutput_Y.Text = String.Format("{0,8:0.00000}", quaternion.Y);
        ScenarioOutput_Z.Text = String.Format("{0,8:0.00000}", quaternion.Z);
        ScenarioOutput_W.Text = String.Format("{0,8:0.00000}", quaternion.W);

        // Rotation Matrix values
        SensorRotationMatrix rotationMatrix = reading.RotationMatrix;
        ScenarioOutput_M11.Text = String.Format("{0,8:0.00000}", rotationMatrix.M11);
        ScenarioOutput_M12.Text = String.Format("{0,8:0.00000}", rotationMatrix.M12);
        ScenarioOutput_M13.Text = String.Format("{0,8:0.00000}", rotationMatrix.M13);
        ScenarioOutput_M21.Text = String.Format("{0,8:0.00000}", rotationMatrix.M21);
        ScenarioOutput_M22.Text = String.Format("{0,8:0.00000}", rotationMatrix.M22);
        ScenarioOutput_M23.Text = String.Format("{0,8:0.00000}", rotationMatrix.M23);
        ScenarioOutput_M31.Text = String.Format("{0,8:0.00000}", rotationMatrix.M31);
        ScenarioOutput_M32.Text = String.Format("{0,8:0.00000}", rotationMatrix.M32);
        ScenarioOutput_M33.Text = String.Format("{0,8:0.00000}", rotationMatrix.M33);
    });
}

版本歷程記錄

Windows 版本 SDK 版本 已新增值
1607 14393 GetDefault (SensorReadingType)
1607 14393 GetDefault (SensorReadingType,SensorOptimizationGoal)
1709 16299 FromIdAsync
1709 16299 GetDeviceSelector (SensorReadingType)
1709 16299 GetDeviceSelector (SensorReadingType,SensorOptimizationGoal)
1709 16299 MaxBatchSize
1709 16299 ReportLatency

屬性

DeviceId

取得裝置識別碼。

MaxBatchSize

取得感應器可批次處理的事件數目上限。

MinimumReportInterval

取得感應器支援的最小報告間隔。

ReadingTransform

取得或設定必須套用至感應器資料的轉換。 要套用的轉換會系結至要對齊感應器資料的顯示方向。

ReadingType

取得感應器讀取類型。

ReportInterval

取得或設定感應器支援的報表間隔。

ReportLatency

取得或設定感應器資訊批次之間的延遲。

方法

FromIdAsync(String)

以非同步方式從其識別碼取得感應器。

GetCurrentReading()

取得目前的感應器讀數。

GetDefault()

傳回 絕對讀數的預設方向感應器。

GetDefault(SensorReadingType)

傳回預設方向感應器,並考慮精確度喜好設定。

GetDefault(SensorReadingType, SensorOptimizationGoal)

傳回預設方向感應器,並考慮電源和精確度喜好設定。

GetDefaultForRelativeReadings()

傳回 相對讀數的預設方向感應器。

GetDeviceSelector(SensorReadingType)

取得裝置選取器。

GetDeviceSelector(SensorReadingType, SensorOptimizationGoal)

取得裝置選取器。

事件

ReadingChanged

每次方向感應器報告新的感應器讀數時發生。

適用於

另請參閱