SimpleOrientationSensor 類別

定義

表示簡單的方向感應器。

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

此感應器會偵測指定裝置目前的象限方向,以及其臉部向上或向下狀態。

public ref class SimpleOrientationSensor 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 SimpleOrientationSensor 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 SimpleOrientationSensor
Public NotInheritable Class SimpleOrientationSensor
繼承
Object Platform::Object IInspectable SimpleOrientationSensor
屬性

Windows 需求

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

備註

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

_sensor = SimpleOrientationSensor.GetDefault();

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

private void ScenarioEnable(object sender, RoutedEventArgs e)
{
    if (_sensor != null)
    {
        Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(VisibilityChanged);
        _sensor.OrientationChanged += new TypedEventHandler<SimpleOrientationSensor, SimpleOrientationSensorOrientationChangedEventArgs>(OrientationChanged);

        ScenarioEnableButton.IsEnabled = false;
        ScenarioDisableButton.IsEnabled = true;

        // Display the current orientation once while waiting for the next orientation change
        DisplayOrientation(ScenarioOutput_Orientation, _sensor.GetCurrentOrientation());
    }
    else
    {
        rootPage.NotifyUser("No simple orientation sensor found", NotifyType.StatusMessage);
    }
}

下列範例包含事件處理常式的 XAML 程式碼,它會將方向讀數寫入應用程式的表單。

async private void OrientationChanged(object sender, SimpleOrientationSensorOrientationChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        DisplayOrientation(ScenarioOutput_Orientation, e.Orientation);
    });
}
private void DisplayOrientation(TextBlock tb, SimpleOrientation orientation)
{
    switch (orientation)
    {
        case SimpleOrientation.NotRotated:
            tb.Text = "Not Rotated";
            break;
        case SimpleOrientation.Rotated90DegreesCounterclockwise:
            tb.Text = "Rotated 90 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Rotated180DegreesCounterclockwise:
            tb.Text = "Rotated 180 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Rotated270DegreesCounterclockwise:
            tb.Text = "Rotated 270 Degrees Counterclockwise";
            break;
        case SimpleOrientation.Faceup:
            tb.Text = "Faceup";
            break;
        case SimpleOrientation.Facedown:
            tb.Text = "Facedown";
            break;
        default:
            tb.Text = "Unknown orientation";
            break;
    }
}

版本歷程記錄

Windows 版本 SDK 版本 已新增值
1809 17763 FromIdAsync
1809 17763 GetDeviceSelector

屬性

DeviceId

取得 SimpleOrientationSensor.FromIdAsync 方法中使用的裝置識別碼。

ReadingTransform

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

方法

FromIdAsync(String)

根據指定的裝置識別碼,以非同步方式擷取 SimpleOrientationSensor 物件。

GetCurrentOrientation()

取得預設的簡單方向感應器。

GetDefault()

取得預設的簡單方向感應器。

GetDeviceSelector()

擷取用來列舉可用 SimpleOrientationSensor 裝置的 AQS) 字串 (進階查詢語法。

事件

OrientationChanged

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

適用於

另請參閱