2016-03-01 68 views
0

我正在做倒計時,應該在計數中顯示一個標籤,但我的viewcontroller上的標籤(lblMostrador)不顯示文本。 代碼正確,因爲輸出框正確顯示計數。 是否有任何特殊配置或標籤工作?標籤不顯示文本 - Xamarin IOS

部分類EsquerdaViewController:UIViewController的 {

private System.Timers.Timer aTimer; 
    private DateTime tempo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 1, 0); 
    private DateTime tempo2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 1); 


    public EsquerdaViewController (IntPtr handle) : base (handle) 
    { 
    } 

    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     Console.WriteLine ("Testando..."); 

     btnVoltar.TouchUpInside += (sebder, e) => 
     { 
      NavigationController.PopViewController(true); 
     }; 


     btnIniciar.TouchUpInside += (sender, e) => 
     { 
      aTimer = new System.Timers.Timer(1000); 

      aTimer.Elapsed += OnTimedEvent; 

      aTimer.AutoReset = true; 

      aTimer.Enabled = true; 

     }; 


    } 

    private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) 
    { 
     System.TimeSpan diff1 = tempo.Subtract(tempo2); 

     if (diff1.Minutes == 0 && diff1.Seconds == 0) 
      aTimer.Stop(); 

     DateTime tempoNovo  = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, diff1.Minutes, diff1.Seconds); 
     tempo     = tempoNovo; 
     Console.WriteLine (String.Format("{0:00}:{1:00}", diff1.Minutes, diff1.Seconds)); 
     lblMostrador.Text = String.Format ("{0:00}:{1:00}", diff1.Minutes, diff1.Seconds); 
    } 
} 

回答

4

當您更新UI對象,你需要確保你是在UI線程上執行:

InvokeOnMainThread (() => { 
    lblMostrador.Text = String.Format ("{0:00}:{1:00}", diff1.Minutes, diff1.Seconds); 
});