标签不显示文本 - Xamarin IOS
本文关键字:Xamarin IOS 文本 显示 标签 | 更新日期: 2023-09-27 18:37:00
我正在做一个倒计时,它应该在计数中显示一个标签,但我的视图控制器上的标签(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);
}
}
更新 UI
对象时,需要确保在 UI 线程上执行:
InvokeOnMainThread ( () => {
lblMostrador.Text = String.Format ("{0:00}:{1:00}", diff1.Minutes, diff1.Seconds);
});