Silverlight学习笔记(六)-----使用DispatcherTimer实现简单计时器--Silverlight学习笔记
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 }
效果图: