Share via


使用陀螺儀

了解如何使用陀螺儀來偵測使用者移動的變化。

重要 API

必要條件

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

您使用的裝置或模擬器必須支援陀螺儀。

建立簡單的陀螺儀應用程式

陀螺儀作為遊戲控制器補充了加速度計。 加速度計可以測量線性運動,而陀螺儀則測量角速度或旋轉運動。

注意

如需更完整的實作,請參閱陀螺儀

指示

  • 建立一個新項目,從 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; // Required to access the core dispatcher object
    using Windows.Devices.Sensors; // Required to access the sensor platform and the gyrometer


    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 Gyrometer _gyrometer; // Our app' s gyrometer object

            // This event handler writes the current gyrometer reading to
            // the three textblocks on the app' s main page.

            private async void ReadingChanged(object sender, GyrometerReadingChangedEventArgs e)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    GyrometerReading reading = e.Reading;
                    txtXAxis.Text = String.Format("{0,5:0.00}", reading.AngularVelocityX);
                    txtYAxis.Text = String.Format("{0,5:0.00}", reading.AngularVelocityY);
                    txtZAxis.Text = String.Format("{0,5:0.00}", reading.AngularVelocityZ);
                });
            }

            public MainPage()
            {
                this.InitializeComponent();
                _gyrometer = Gyrometer.GetDefault(); // Get the default gyrometer sensor object

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

                    // Assign an event handler for the gyrometer reading-changed event
                    _gyrometer.ReadingChanged += new TypedEventHandler<Gyrometer, GyrometerReadingChangedEventArgs>(ReadingChanged);
                }

            }
        }
    }

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

  • 開啟檔案 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="23" Margin="8,8,0,0" TextWrapping="Wrap" Text="X-Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFDFDFD"/>
            <TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="23" Margin="67,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="88" Foreground="#FFFDFAFA"/>
            <TextBlock HorizontalAlignment="Left" Height="20" Margin="8,52,0,0" TextWrapping="Wrap" Text="Y Axis:" VerticalAlignment="Top" Width="46" Foreground="White"/>
            <TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="24" Margin="54,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80" Foreground="#FFFBFBFB"/>
            <TextBlock HorizontalAlignment="Left" Height="21" Margin="8,93,0,0" TextWrapping="Wrap" Text="Z Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFEFBFB"/>
            <TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="21" Margin="54,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="63" Foreground="#FFF8F3F3"/>

        </Grid>
    </Page>

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

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

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

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

說明

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

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

_gyrometer = Gyrometer.GetDefault(); // Get the default gyrometer sensor object

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

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

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

_gyrometer.ReadingChanged += new TypedEventHandler<Gyrometer,
GyrometerReadingChangedEventArgs>(ReadingChanged);

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

        <TextBlock HorizontalAlignment="Left" Height="23" Margin="8,8,0,0" TextWrapping="Wrap" Text="X-Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFDFDFD"/>
        <TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="23" Margin="67,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="88" Foreground="#FFFDFAFA"/>
        <TextBlock HorizontalAlignment="Left" Height="20" Margin="8,52,0,0" TextWrapping="Wrap" Text="Y Axis:" VerticalAlignment="Top" Width="46" Foreground="White"/>
        <TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="24" Margin="54,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80" Foreground="#FFFBFBFB"/>
        <TextBlock HorizontalAlignment="Left" Height="21" Margin="8,93,0,0" TextWrapping="Wrap" Text="Z Axis:" VerticalAlignment="Top" Width="46" Foreground="#FFFEFBFB"/>
        <TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="21" Margin="54,93,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="63" Foreground="#FFF8F3F3"/>