0

我尝试获取 xamarin 开发的 crossplateform 应用程序的磁场数据。我是移动开发的初学者,尤其是 Xamarin。所以我有 Hello Word 代码:

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 个坐标值。

Device Motion Plugin 是一个多平台插件,我也有这段代码,但我不知道如何在应用程序中显示这些值。

        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;

            }
        };
4

1 回答 1

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
);
于 2017-03-18T14:21:38.327 回答