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;

    // Required to support the core dispatcher and the accelerometer

    using Windows.UI.Core;
    using Windows.Devices.Sensors;

    namespace App1
    {

        public sealed partial class MainPage : Page
        {
            // Sensor and dispatcher variables
            private Accelerometer _accelerometer;

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

            private async void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    AccelerometerReading reading = e.Reading;
                    txtXAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
                    txtYAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
                    txtZAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);

                });
            }

            public MainPage()
            {
                this.InitializeComponent();
                _accelerometer = Accelerometer.GetDefault();

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

                    // Assign an event handler for the reading-changed event
                    _accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);
                }
            }
        }
    }

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

  • 開啟檔案 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="25" Margin="8,20,0,0" TextWrapping="Wrap" Text="X-axis:" VerticalAlignment="Top" Width="62" Foreground="#FFEDE6E6"/>
            <TextBlock HorizontalAlignment="Left" Height="27" Margin="8,49,0,0" TextWrapping="Wrap" Text="Y-axis:" VerticalAlignment="Top" Width="62" Foreground="#FFF5F2F2"/>
            <TextBlock HorizontalAlignment="Left" Height="23" Margin="8,80,0,0" TextWrapping="Wrap" Text="Z-axis:" VerticalAlignment="Top" Width="62" Foreground="#FFF6F0F0"/>
            <TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="15" Margin="70,16,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="61" Foreground="#FFF2F2F2"/>
            <TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="15" Margin="70,49,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFF2EEEE"/>
            <TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="15" Margin="70,80,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFFFF8F8"/>

        </Grid>
    </Page>

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

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

應用程式執行之後,您可以移動裝置或使用模擬器工具來變更加速計值。

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

說明

上一個範例示範您需要撰寫多少程式碼,才能在應用程式中整合加速計輸入。

應用程式會使用 MainPage 方法中的預設加速計建立連線。

_accelerometer = Accelerometer.GetDefault();

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

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

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

_accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer,
AccelerometerReadingChangedEventArgs>(ReadingChanged);

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

<TextBlock x:Name="txtXAxis" HorizontalAlignment="Left" Height="15" Margin="70,16,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="61" Foreground="#FFF2F2F2"/>
 <TextBlock x:Name="txtYAxis" HorizontalAlignment="Left" Height="15" Margin="70,49,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFF2EEEE"/>
 <TextBlock x:Name="txtZAxis" HorizontalAlignment="Left" Height="15" Margin="70,80,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="53" Foreground="#FFFFF8F8"/>