Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在使用片段和计时器的选项卡式活动上更新UI_C#_Android_Xamarin.android_System.timers.timer - Fatal编程技术网

C# 如何在使用片段和计时器的选项卡式活动上更新UI

C# 如何在使用片段和计时器的选项卡式活动上更新UI,c#,android,xamarin.android,system.timers.timer,C#,Android,Xamarin.android,System.timers.timer,我有一个带有3个片段的选项卡式活动。第一个片段允许用户选择一个测试,第三个片段有一个计时器、一个文本视图和一些按钮。第二个碎片到现在为止没有任何东西 在片段3中,我有一个启动计时器的按钮,一旦计时器启动,textview每分钟刷新一次,显示经过的时间。到目前为止,一切都很顺利 问题:一旦计时器启动,如果我选择片段1并返回片段3,计时器将停止更新textView。我知道计时器运行正常,但textview没有更新 我尝试过FragmentActivity.RunOnUiThread=>{};在切换片

我有一个带有3个片段的选项卡式活动。第一个片段允许用户选择一个测试,第三个片段有一个计时器、一个文本视图和一些按钮。第二个碎片到现在为止没有任何东西

在片段3中,我有一个启动计时器的按钮,一旦计时器启动,textview每分钟刷新一次,显示经过的时间。到目前为止,一切都很顺利

问题:一旦计时器启动,如果我选择片段1并返回片段3,计时器将停止更新textView。我知道计时器运行正常,但textview没有更新

我尝试过FragmentActivity.RunOnUiThread=>{};在切换片段之前,此操作可以正常工作

我确实尝试过使用Loopers.MainLooper更新屏幕,但问题仍然没有改变

// update the screen every minute
                if (mActivity != null)
                { 
                    mActivity.RunOnUiThread(() =>
                    {

                        // set the progress bar
                        progressBar.Progress = i32ProgressBarValue;
                        textViewPercentage.Text = i32ProgressBarValue + "%";

                        // set the text view
                        textViewTestTime.Text = $"{Globals.i32Days}" + "D :" + $"{Globals.i32Hours}" + "H :" + $"{Globals.i32Mins}" + "M";
                    });
                }

我希望在切换片段并返回片段3时,textView能够继续正确更新。我在选项卡ActivityUse BottomNavigationView中创建了一个示例使用计时器,它也有三个片段,在第三个片段中,我使用按钮每两秒启动更新按钮文本的计时器,它在切换片段时也能很好地工作,下面是第三个片段中的代码,您可以参考它:

Timer _dispatcherTimer;
TimerCallback timerDelegate;

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
        View view = inflater.Inflate(Resource.Layout.fragment_account, container, false);
        init(view);
        timerDelegate = new TimerCallback(Tick);

        return view;
    }
   private void init(View view)
    {
        button = view.FindViewById<Button>(Resource.Id.mybutton);
        button.Click += delegate
        {
            _dispatcherTimer = new System.Threading.Timer(timerDelegate, null, 0, 2000);
        };
    }
    private void Tick(object state)
    {

        this.Activity.RunOnUiThread(() =>
        {
            //do something
              Random reRandom = new Random();
              int s = reRandom.Next(1000);
                button.Text = s.ToString();
        });
    }

谢谢你的解决方案!回调计时器工作正常。我不得不对代码进行一些小的调整,以使其正常工作。每次我们返回片段3时,_dispatchedTimer结果都是null,因此在OnCreateView的返回视图之前添加了以下行。。让它工作:_dispatchedTimer.Dispose;and dispatchermer=new System.Threading.TimerGlobals.timerDelegate,null,0,2000;这样,计时器间隔不会增加,屏幕开始更新。谢谢你的帮助!由于我们正在切换片段,因此如果此.Activity!=在更新前为nullUI@SwathiAyas是的,我的样品适合我的需要,应该根据您自己的项目进行调整。如果它对你有帮助,你能标记一下吗?谢谢