2016-07-07 16 views
0

我嘗試獲取由xamarin開發的crossplateform應用程序的磁場數據。我非常喜歡手機開發,特別是Xamarin。 所以我有你好三字代碼:如何使用Device Motion插件使用xamarin獲取磁力計值?

public App() 
     { 
      // The root page of your application 
       MainPage = new ContentPage { 

      Content = new StackLayout {   
        VerticalOptions = LayoutOptions.Center, 
        Children = { 
         new Label { 

          XAlign = TextAlignment.Center, 
         Text = "Welcome to Xamarin Forms!" 
         } 
        } 
       } 

      }; 


     } 

我想顯示3座標在第一頁的magnetometre的值。

設備運動插件是一個multiplateform插件,我也有這個代碼,但我不知道如何做,以在應用程序中顯示這些值。

 CrossDeviceMotion.Current.Start(MotionSensorType.Magnetometer); 
     CrossDeviceMotion.Current.SensorValueChanged += (s, a) => 
     { 

      switch (a.SensorType) 
      { 

       case MotionSensorType.Magnetometer: 
        Debug.WriteLine("A: {0},{1},{2}", ((MotionVector)a.Value).X, ((MotionVector)a.Value).Y, ((MotionVector)a.Value).Z); 

        break; 

      } 
     }; 

回答

0

爲了達到此目的,您需要設置x:Name屬性標籤。我一直在使用加速計,所以我的名字是accelerometerLog

然後從你的代碼,你可以做到以下幾點:

accelerometerLog.Text = String.Format(
    "X:{0} Y:{1} Z:{2}", 
    ((MotionVector)a.Value).X, 
    ((MotionVector)a.Value).Y, 
    ((MotionVector)a.Value).Z 
);