Ios 在Xamarin中使用NSTimer更新按钮控件

Ios 在Xamarin中使用NSTimer更新按钮控件,ios,timer,xamarin,nstimer,Ios,Timer,Xamarin,Nstimer,我创建了一个非常基本的ios项目,上面有一个按钮,其标题文本属性设置为“开始”。我正试着让这个按钮从15倒计时 我为按钮添加了事件处理程序,如下所示 void StartButton_TouchUpInside (object sender, EventArgs ea) { _currentCount = 15; _timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1

我创建了一个非常基本的ios项目,上面有一个按钮,其标题文本属性设置为“开始”。我正试着让这个按钮从15倒计时

我为按钮添加了事件处理程序,如下所示

     void StartButton_TouchUpInside (object sender, EventArgs ea) {

        _currentCount = 15;

        _timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1), () =>
            {

                if (_currentCount <= 0) {
                    StartButton.TitleLabel.Text = "Start";
                    _timer.Dispose();
                } else { 
                    string titleText = (_currentCount--).ToString ();
                    //Console.WriteLine(titleText);
                    StartButton.TitleLabel.Text = titleText;
                    //Console.WriteLine(StartButton.TitleLabel.Text);
                    //StartButton.SetNeedsDisplay();
                }
            });
    }
void StartButton\u触控内部(对象发送器、事件参数){
_电流计数=15;
_timer=NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1),()=>
{

如果(_currentCount尝试使用this.InvokeOnMainThread

  private int counter = 0;
  public override void WillActivate()
  {        
     Console.WriteLine ("{0} will activate", this);
     _timer = NSTimer.CreateRepeatingScheduledTimer(1, (timer) =>
     {
        this.InvokeOnMainThread(()=>{
           Console.WriteLine("++++++++ NSTimer Call");
           this.AlertTimeLabel.SetText(counter.ToString());
           counter++;
        });
     });
  }

尝试将count变量放入计时器delgate代码中