Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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# label.Text将只更新一次_C#_Xamarin - Fatal编程技术网

C# label.Text将只更新一次

C# label.Text将只更新一次,c#,xamarin,C#,Xamarin,我正在运行计时器并计时。计时器以按钮开始,标签显示时间。至少这是计划。 但是运行代码时,label.Text只会更新一次。它从6:00开始,会滴答一次到5:59,然后会冻结,为什么会发生这种情况 我花了那么多时间试图了解发生了什么。可变时间正在更改,而label.Text不更改。我做错什么了吗 public partial class Running : ContentPage { Timer timer; double seconds = 360; public Ru

我正在运行计时器并计时。计时器以按钮开始,标签显示时间。至少这是计划。 但是运行代码时,label.Text只会更新一次。它从6:00开始,会滴答一次到5:59,然后会冻结,为什么会发生这种情况

我花了那么多时间试图了解发生了什么。可变时间正在更改,而label.Text不更改。我做错什么了吗

public partial class Running : ContentPage
{
    Timer timer;
    double seconds = 360;

    public Running()
    {
        InitializeComponent();
        button_run.Clicked += Button_Run_Clicked;
    }

    void Button_Run_Clicked(object sender, EventArgs e)
    {
        if (button_run.Text == "Start!")
        {
            button_run.Text = "Stop";
            timer = new Timer();
            timer.Interval = 100; // 100 milliseconds  
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        else
        {
            button_run.Text = "Start!";
        }


    }

    private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        seconds -= 0.1;
        int minutes = (int)seconds / 60;
        int secs = (int)seconds % 60;
        string time = minutes.ToString() + ":" + secs.ToString();
        label_timer.Text = time; // the string time is changing!

        if (minutes != 0)
        {
            timer.Start();
        }
        else
        {
            timer.Stop();
        }
    }
}

UI更新只能在主线程上完成

Device.BeginInvokeOnMainThread( () => {
  label_timer.Text = time;
});