简体   繁体   English

为标签添加动态时间

[英]Add a dynamic time for my label

I have a label in my grid 我在网格中有一个标签

<Label Name="time"  Grid.Column="5" Grid.Row="1"   />

And then 接着

 time.Content = DateTime.Now.ToString();

But it's static. 但这是静态的。

How can I make the time label update to represent the actual time? 如何使时间标签更新以代表实际时间?

On the constructor of your page : 在页面的构造函数中:

DispatcherTimer messageTimer = new DispatcherTimer(); 
messageTimer.Tick += new EventHandler(messageTimer_Tick); 
messageTimer.Interval = new TimeSpan(0, 0, 1); 
messageTimer.Start();

and the messageTimer_Tick : 和messageTimer_Tick:

void messageTimer_Tick(object sender, EventArgs e)
{
    time.Content = DateTime.Now.ToString();
}

您必须在页面上添加Timer控件,每隔一秒就会更新GridView

public Timer()
{
        timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
        timer.Interval = (1000) * (1);              // Timer will tick evert second
        timer.Enabled = true;                       // Enable the timer
        timer.Start();                              // Start the timer
}

Now every second the event will be launched and the following EventHandler will generate the current date and set this as the value of time.Content, which will cause the dynamic effect. 现在,将每秒启动一次事件,随后的EventHandler将生成当前日期,并将其设置为time.Content的值,这将引起动态效果。

void timer_Tick(object sender, EventArgs e)
{
        time.Content = DateTime.Now.ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM