c#变量未更新

问题描述 投票:0回答:1

我正在尝试创建一个Xamarin Android应用程序,在其中记录定向传感器数据。我设置了一个计时器,该计时器运行10毫秒,然后重新启动400次。

public void rogzit_Clicked(object sender, EventArgs e)        
        {                                           
            timer1 = new Timer();

            timer1.Interval = 10;
            timer1.Elapsed += Timer1_Elapsed;


            if (OrientationSensor.IsMonitoring)
            {
                rogzit.Enabled = false;
                for (int i = 0; i < 400; i++)
                {
                    jel = 1;
                    timer1.Start();

                    while (jel != 0)
                    {

                    }

                }               
                string mappa = Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath;
                rogzit.Enabled = true;
                System.IO.File.WriteAllLines(mappa, array);
            }                       
        }
        private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
        {

            timer1.Stop();
            idoo[db] = Convert.ToString(db * 0.01);
            meres[db] = szog.ToString("f2");
            array[db] = string.Format("{0} {1}", idoo[db], meres[db]);
            db += 1;
            jel = 0;
        }

变量szog应该更改,但保持不变。

public void OrientationSensor_Readingchanged(object sender, OrientationSensorChangedEventArgs e)
        {
            xQ = e.Reading.Orientation.X;
            yQ = e.Reading.Orientation.Y;
            zQ = e.Reading.Orientation.Z;
            wQ = e.Reading.Orientation.W;

            double sinr_cosp = +2.0 * (wQ * xQ + yQ * zQ);
            double cosr_cosp = +1.0 - 2.0 * (xQ * xQ + yQ * yQ);
            xAngle = Math.Atan2(sinr_cosp, cosr_cosp);

            double sinp = +2.0 * (wQ * yQ - zQ * xQ);



            if (Math.Abs(sinp) >= 1)
            {
                if (sinp>0)
                {
                    yAngle = (Math.PI / 2);
                }
                else
                {
                    yAngle = -1 * (Math.PI / 2);
                }

            }
            else
            {
                yAngle = Math.Asin(sinp);
            }

            double siny_cosp = +2.0 * (wQ *zQ + xQ * yQ);
            double cosy_cosp = +1.0 - 2.0 * (yQ * yQ + zQ * zQ);
            zAngle = Math.Atan2(siny_cosp, cosy_cosp);

            double elojel;

            if (xAngle > 0)
            {
                elojel = 1;
            }
            else if (xAngle < 0)
            {
                elojel = -1;
            }
            else
            {
                elojel = 0;
            }

            szog = Math.Sqrt(yAngle * yAngle + xAngle * xAngle)*elojel;
            pf = szog.ToString("f2");
            szogErtek.Text = string.Format("{0} [rad]", pf);

Orientationsensor_Readingchanged中,我给szog赋了一个值,该值随方向而变化。但是它保持与rogzit_Clicked开始时相同的值,并记录该值400次。

timer xamarin.android orientation recording
1个回答
0
投票

szogErtek.Text = string.Format("{0} [rad]", pf);行似乎未在UI线程上被调用,因为它位于事件侦听器内部。您必须使用Activity.RunOnUiThread将更新UI的任务部分移动到主线程上。因此,它必须看起来像这样:

Activity.RunOnUiThread(szogErtek.Text = string.Format("{0} [rad]", pf));
© www.soinside.com 2019 - 2024. All rights reserved.