Silverlight学习笔记(六)-----使用DispatcherTimer实现简单计时器--Silverlight学习笔记

本文展示了一个使用WPF实现的简单计时器示例。该示例通过XAML定义了界面布局,并使用C#实现了定时更新界面上显示的时间。代码中包含了XAML布局配置和C#逻辑处理部分。

XAML:

 1 <Grid x:Name="LayoutRoot" Background="White"
2 Width="640" Height="360">
3 <!--Background-->
4 <Rectangle Fill="Gold" Stroke="Black"
5 StrokeThickness="3"
6 RadiusX="5" RadiusY="5"/>
7 <!--show the time-->
8 <TextBlock x:Name="tbkTimer"
9 Width="300" Height="50"
10 FontSize="16" Foreground="Red"/>
11 </Grid>



C#:

 1 public partial class MainPage : UserControl
2 {
3 public MainPage()
4 {
5 InitializeComponent();
6 // new a DispatcherTimer
7 DispatcherTimer timer = new DispatcherTimer();
8 //set the interval
9 timer.Interval = new TimeSpan(0, 0, 1);
10 // event
11 timer.Tick+=new EventHandler(timer_Tick);
12 timer.Start();
13 }
14
15 private void timer_Tick(object sender, EventArgs e)
16 {
17 //show the time
18 tbkTimer.Text = "当前时间: " + DateTime.Now.ToLongTimeString();
19 }
20 }



效果图:



转载于:https://www.cnblogs.com/crazypig/archive/2012/01/14/2322424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值