使用傾角計

了解如何使用傾斜計來判斷傾斜度、滾筒和偏轉。

重要 API

必要條件

您應該熟悉可擴充應用程式標記語言 (XAML)、Microsoft Visual C# 和事件。

您使用的裝置或模擬器必須支援傾斜計。

建立一個簡單的傾角儀應用程式

有些 3D 遊戲需要傾斜儀作為輸入裝置。 一個常見的例子是飛行模擬器,它將傾斜計的三個軸 (X、Y 和 Z) 對應到飛機的升降舵、副翼和方向舵輸入。

注意

有關更完整的實現,請參閱測斜儀範例

指示

  • 建立一個新項目,從 Visual C# 項目範本中選擇一個空白應用程式 (通用 Windows)

  • 開啟專案的 MainPage.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;
    using Windows.Devices.Sensors;


    namespace App1
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            private Inclinometer _inclinometer;

            // This event handler writes the current inclinometer reading to
            // the three text blocks on the app' s main page.

            private async void ReadingChanged(object sender, InclinometerReadingChangedEventArgs e)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    InclinometerReading reading = e.Reading;
                    txtPitch.Text = String.Format("{0,5:0.00}", reading.PitchDegrees);
                    txtRoll.Text = String.Format("{0,5:0.00}", reading.RollDegrees);
                    txtYaw.Text = String.Format("{0,5:0.00}", reading.YawDegrees);
                });
            }

            public MainPage()
            {
                this.InitializeComponent();
                _inclinometer = Inclinometer.GetDefault();


                if (_inclinometer != null)
                {
                    // Establish the report interval for all scenarios
                    uint minReportInterval = _inclinometer.MinimumReportInterval;
                    uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
                    _inclinometer.ReportInterval = reportInterval;

                    // Establish the event handler
                    _inclinometer.ReadingChanged += new TypedEventHandler<Inclinometer, InclinometerReadingChangedEventArgs>(ReadingChanged);
                }
            }
        }
    }

您必須使用您提供項目的名稱,重新命名上一個代碼段中的命名空間。 例如,如果您已建立名為 AccelerometerCS 的專案,則會將 namespace App1 取代為 namespace InclinometerCS

  • 開啟檔案 MainPage.xaml 並將原始內容替換為以下 XML。
        <Page
        x:Class="App1.MainPage"
        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="#FF0C0C0C">
            <TextBlock HorizontalAlignment="Left" Height="21" Margin="0,8,0,0" TextWrapping="Wrap" Text="Pitch: " VerticalAlignment="Top" Width="45" Foreground="#FFF9F4F4"/>
            <TextBlock x:Name="txtPitch" HorizontalAlignment="Left" Height="21" Margin="59,8,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="71" Foreground="#FFFDF9F9"/>
            <TextBlock HorizontalAlignment="Left" Height="23" Margin="0,29,0,0" TextWrapping="Wrap" Text="Roll:" VerticalAlignment="Top" Width="55" Foreground="#FFF7F1F1"/>
            <TextBlock x:Name="txtRoll" HorizontalAlignment="Left" Height="23" Margin="59,29,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="50" Foreground="#FFFCF9F9"/>
            <TextBlock HorizontalAlignment="Left" Height="19" Margin="0,56,0,0" TextWrapping="Wrap" Text="Yaw:" VerticalAlignment="Top" Width="55" Foreground="#FFF7F3F3"/>
            <TextBlock x:Name="txtYaw" HorizontalAlignment="Left" Height="19" Margin="55,56,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="54" Foreground="#FFF6F2F2"/>

        </Grid>
    </Page>

您必須將上一個代碼段中類別名稱的第一個部分取代為應用程式的命名空間。 例如,如果您已建立名為 AccelerometerCS 的專案,則會將 x:Class="App1.MainPage" 取代為 x:Class="InclinometerCS.MainPage"。 您也應該將取代 xmlns:local="using:App1"xmlns:local="using:InclinometerCS"

  • 按 F5 或選擇偵錯>或開始偵錯來建置、部署和執行應用程式。

應用程式執行後,您可以透過行動裝置或使用模擬器工具來變更傾斜計值。

  • 返回 Visual Studio 並按 Shift+F5,或選取偵錯>停止偵錯以停止應用程式,以停止應用程式。

說明

前面的範例示範了您只需編寫很少的程式碼即可將傾斜計輸入整合到您的應用程式中。

應用程式會使用 MainPage 方法中的預設傾斜儀建立連線。

_inclinometer = Inclinometer.GetDefault();

應用程式會在 MainPage 方法內建立報表間隔。 此程式碼會擷取裝置支援的最小間隔,並將它與要求間隔 16 毫秒 (大約 60-Hz 重新整理速率) 進行比較。 如果支援的最小間隔大於要求的間隔,程式碼會將值設定為最小值。 否則,它會將值設定為要求的間隔。

uint minReportInterval = _inclinometer.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_inclinometer.ReportInterval = reportInterval;

新的傾斜儀資料會在 ReadingChanged 方法中擷取。 每次感應器驅動程式收到來自感應器的新資料時,都會使用此事件處理常式將值傳遞給您的應用程式。 應用程式會在下一行註冊此事件處理常式。

_inclinometer.ReadingChanged += new TypedEventHandler<Inclinometer,
InclinometerReadingChangedEventArgs>(ReadingChanged);

這些新值會寫入專案 XAML 中找到的 TextBlock。

<TextBlock HorizontalAlignment="Left" Height="21" Margin="0,8,0,0" TextWrapping="Wrap" Text="Pitch: " VerticalAlignment="Top" Width="45" Foreground="#FFF9F4F4"/>
 <TextBlock x:Name="txtPitch" HorizontalAlignment="Left" Height="21" Margin="59,8,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="71" Foreground="#FFFDF9F9"/>
 <TextBlock HorizontalAlignment="Left" Height="23" Margin="0,29,0,0" TextWrapping="Wrap" Text="Roll:" VerticalAlignment="Top" Width="55" Foreground="#FFF7F1F1"/>
 <TextBlock x:Name="txtRoll" HorizontalAlignment="Left" Height="23" Margin="59,29,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="50" Foreground="#FFFCF9F9"/>
 <TextBlock HorizontalAlignment="Left" Height="19" Margin="0,56,0,0" TextWrapping="Wrap" Text="Yaw:" VerticalAlignment="Top" Width="55" Foreground="#FFF7F3F3"/>
 <TextBlock x:Name="txtYaw" HorizontalAlignment="Left" Height="19" Margin="55,56,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="54" Foreground="#FFF6F2F2"/>