我正在进行倒计时,应在计数中显示标签,但我的viewcontroller上的标签(lblMostrador)不会显示文本。 代码是正确的,因为输出框正确显示计数。 是否有任何特殊配置或标签工作?
partial class 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);
}
}
答案 0 :(得分:4)
更新UI对象时,需要确保在UI线程上执行:
InvokeOnMainThread ( () => {
lblMostrador.Text = String.Format ("{0:00}:{1:00}", diff1.Minutes, diff1.Seconds);
});