簡體   English   中英

如何在標簽中顯示計時器的剩余時間?

[英]How can I show in a label the remaining time of a timer?

我有一個計時器,可以在45秒內啟用一個按鈕

 Device.StartTimer(new TimeSpan(0, 0, 45), EnableButtonResend);


 private bool EnableButtonResend()
    {
        IsEnabledResend = true;
        return true;
    }

一切正常,我想在標簽上顯示啟用按鈕之前的剩余時間。 例如,請等待XX秒鍾,然后重新發送。 有人知道怎么做嗎? 謝謝你的時間

您可以花時間啟用一個變量:

DateTime dt = DateTime.Now.AddSeconds(45);
Device.StartTimer(new TimeSpan(0, 0, 45), EnableButtonResend);
SownRemaining();

而在該時間還沒有到時,請調用更改標簽文本的方法:

void ShowRemaing()
{
    int a = (int)((dt - DateTime.Now).TotalSeconds);
    if(a > 0)
    {
    SomeLabel.Text = $"{a} Seconds to enable";
    Device.StartTimer(new TimeSpan(0, 0, 1), ShowRemaing);
    }
}

 private bool EnableButtonResend()
    {
        IsEnabledResend = true;
        return true;
    }

像這樣嗎

Device.StartTimer(new TimeSpan(0, 0, 1), EnableButtonResend);

int count = 0;

private bool EnableButtonResend()
{ 
    if(count < 45) {
        IsEnabledResend = true;
        count++;
        Button.SetText((45 - count) + " seconds remaining");
        return true;
    }
    // Do your stuff or reset state
    count = 0;
    return false;
}

如果在EnableButtonResend返回true ,則計時器將不斷重復,因此請設置每秒的時間間隔,然后您將有一個回調以在需要時更改標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM