【问题标题】:How can I get magnetometer values with xamarin using the Device Motion Plugin?如何使用 Device Motion 插件通过 xamarin 获取磁力计值?
【发布时间】:2016-07-07 08:39:05
【问题描述】:

我尝试获取 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;

            }
        };

【问题讨论】:

    标签: c# cross-platform xamarin.forms devicemotion


    【解决方案1】:

    为了使其工作,您需要设置标签的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
    );
    

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多