当前位置:编程学习 > wap >>

简单的计时器WP7 DispatcherTimer

做一个每秒都刷新的time。

相当于2012/3/7 22:09:31时间每秒刷新一次

其实非常简单,现在只展示silverlight phone的操作

效果如下

  \

 

代码首先添加一个TextBlock。其实只要是显示空间都行。
只是TextBlock效果更好点www.zzzyk.com
1        <TextBlock Name="myTextBox" HorizontalAlignment="Center" VerticalAlignment="Center" />
显示都为居中。name叫myTextBox
  View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13 using System.Windows.Threading;
14
15 namespace helloWord
16 {
17     public partial class Page3 : PhoneApplicationPage
18     {
19         public Page3()
20         {
21             InitializeComponent();
22             DispatcherTimer timer = new DispatcherTimer();
23             timer.Interval = TimeSpan.FromSeconds(1);
24             timer.Tick += timer_Tick;
25             timer.Start();
26         }
27
28         void timer_Tick(object sender, EventArgs e)
29         {
30             this.myTextBox.Text = DateTime.Now.ToString();
31         }
32     }
33 }

这个代码其实也没什么可说的。
无非就是
 DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();
这四句代码
第一句是实例化DispatcherTimer
第二句是获取timespan时间格式。每隔1秒刷新
javascript中也有 interval和settimeout。interval就是每次隔多久刷新一次(多次)
settimeout是隔多久刷新一次(单次)
javascript中:不过interval和settimeout最本质的区别是interval比settimeout快。因为它每次都执行新的function
 
然后运行。我们的例子就搞定啦。

 

摘自  wojiuzhuai
 
补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,