WPF 显示时间的两种方法
(2012-05-16 14:22:26)
前面已经有一篇博文<C#
获得当前日期和星期>已经提到了显示时间的方法,前些天又发现另一种方法,今天得空把两种方法写在一起,一来能作个比较,二来权当温习功课吧.
方法一:
1.先定义一个方法,在这个方法里,把时间赋值给控件
private
void TimerTick(object sender, EventArgs e)
{
lblTime.Content
= DateTime.Now.ToLongTimeString();
}
2.启动一个Timer,它的做用就是每一秒更新一下时间
private
void LaunchTimer()
{
//
判断是否在设计模式,是则返回,注意:如果不加这句话,正在设计界面的时候可能会报错哦!
if
(System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
return;
}
DispatcherTimer
timer = new DispatcherTimer();
timer.Interval
= new TimeSpan(0, 0, 1);
//间隔1秒
timer.Tick
+= new EventHandler(TimerTick);
timer.Start();
}
方法二:
1.先定义一个方法,在这个方法里,把时间赋值给控件
private
void TimerTick(object sender, EventArgs e)
{
lblTime.Content
= DateTime.Now.ToLongTimeString();
}
2.启动一个Timer,它的做用就是每一秒更新一下时间
private
void LaunchTimer()
{
DispatcherTimer
innerTimer = new
DispatcherTimer(TimeSpan.FromSeconds(1.0),
DispatcherPriority.Loaded,
new EventHandler(this.InnerTimerCallback), this.Dispatcher);
innerTimer.Start();
}
最后不要忘了,在适当的位置调用LaunchTimer()哦!!!
喜欢
0
赠金笔
加载中,请稍候......