c# - 标签不显示文本 - Xamarin IOS

标签 c# ios xamarin

我正在做一个倒计时,应该在计数中显示一个标签,但我的 View Controller 上的标签 (lblMostrador) 不显示文本。 代码是正确的,因为输出框正确显示了计数。 标签工作是否有特殊配置或其他?

部分类 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);
});

关于c# - 标签不显示文本 - Xamarin IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733688/

相关文章:

c# - 从 javascript 调用 asp.net 页面方法不起作用

c# - 我怎样才能从这个 C# 代码中获取基本文件名?

ios - 在文本字段之间更改焦点时保持键盘打开

ios - 我如何只使用 Storyboard而不使用代码来设置带有自动布局的 uitableViewcell 高度?

iOS - 按下按钮后更改按钮颜色不起作用

c# - Math.Sqrt() 的时间复杂度?

c# - 如何在 Teamcity 上使用 ApprovalTests?

java - 将 Android 自定义 ImageView 转换为 Xamarin.Android

c# - NETSDK1061 : The project was restored using Microsoft. NETCore.App 版本1.0.0,但使用当前设置,将使用版本2.0.9

c# - 跳过 X 帧