前提・実現したいこと
Xamarin学習中です。
NugetからXam.Plugin.DeviceMotionをインストールして、
加速度センサーとジャイロセンサーの値を取得してXAMLにデータバインディングで表示させているのですが、
どんな動かし方をしても加速度センサーの値とジャイロセンサーの値が全く同じになってしまいます。
エミュレータ上でも実機でも同じです。
ソースコード
加速度センサー
ViewModel
1public void StartACCEL(){ 2 motion.Start(MotionSensorType.Accelerometer, MotionSensorDelay.Ui); 3 if (motion.IsActive(MotionSensorType.Accelerometer)) 4 { 5 motion.SensorValueChanged += (object s, SensorValueChangedEventArgs a) => 6 { 7 8 Device.BeginInvokeOnMainThread(() => 9 { 10 this.xAccel = ((MotionVector)a.Value).X.ToString("0.0000"); 11 this.yAccel = ((MotionVector)a.Value).Y.ToString("0.0000"); 12 this.zAccel = ((MotionVector)a.Value).Z.ToString("0.0000"); 13 }); 14 }; 15 } 16 }
ジャイロセンサー
ViewModel
1public void StartGYRO() 2 { 3 motion.Start(MotionSensorType.Gyroscope, MotionSensorDelay.Default); 4 if (motion.IsActive(MotionSensorType.Gyroscope)) 5 { 6 motion.SensorValueChanged += (object s, SensorValueChangedEventArgs v) => 7 { 8 Device.BeginInvokeOnMainThread(() => 9 { 10 this.xGyro = ((MotionVector)v.Value).X.ToString("0.0000"); 11 this.yGyro = ((MotionVector)v.Value).Y.ToString("0.0000"); 12 this.zGyro = ((MotionVector)v.Value).Z.ToString("0.0000"); 13 }); 14 }; 15 } 16 } 17
どちらも同じViewModel内に記述しています(動作が安定したらModelに分割するつもりです)
それぞれのセンサー値のx、y、zの値をString型に変換してデータバインディングでViewに表示させています。
試したこと
どちらもメインスレッド上で動くのが原因なのかと思い、Device.BeginInvokeOnMainThread(() =>
を削除したりしましたが結果は変わりませんでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/30 00:49