且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone 7 利用计时器DispatcherTimer创建时钟

更新时间:2022-10-03 10:25:41

DispatcherTimer是在System.Windows.Threading 命名空间下的定时器。集成到按指定时间间隔和指定优先级处理的 Dispatcher 队列中的计时器。 在每个 Dispatcher 循环的顶端重新计算 DispatcherTimer。 不能保证会正好在时间间隔发生时执行计时器,但能够保证不会在时间间隔发生之前执行计时器。 这是因为 DispatcherTimer 操作与其他操作一样被放置到 Dispatcher 队列中。 何时执行 DispatcherTimer 操作取决于队列中的其他作业及其优先级。

每当将对象方法绑定到计时器时,DispatcherTimer 都将使对象保持活动状态。 
DispatcherTimer timer = new DispatcherTimer() ;

timer.Tick += new EventHandler(timer_Tick);

void timer_Tick()

{}

timer.Interval = new TimeSpan(TimeSpan.TicksPerSecond);

首先,DispatcherTimer只会创建一个线程,竟然是一个线程,那么它的执行顺序就很明显,要等执行完成之后,才会重头开始执行。

其实是这样的,每当线程执行完一次,等待Interval 设置的时间(比如上面我们设置了TicksPerSecond),然后才会重新开始,也就是说

,要执行完->然后再等待时间->然后重复

时钟的例子:

 


  1. <Grid x:Name="LayoutRoot" Background="Transparent"> 
  2.         <Grid.RowDefinitions> 
  3.             <RowDefinition Height="Auto"/> 
  4.             <RowDefinition Height="*"/> 
  5.         </Grid.RowDefinitions> 
  6.  
  7.         <!--TitlePanel contains the name of the application and page title--> 
  8.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
  9.             <TextBlock x:Name="ApplicationTitle" Text="利用定时器设计的时钟" Style="{StaticResource PhoneTextNormalStyle}"/> 
  10.         </StackPanel> 
  11.  
  12.         <!--ContentPanel - place additional content here--> 
  13.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"   
  14.               SizeChanged="OnContentPanelSizeChanged"> 
  15.             <TextBlock Name="referenceText" 
  16.                        Text="参考对象文本控件" 
  17.                       /> 
  18.  
  19.             <TextBlock Name="hourHand"> 
  20.                 <TextBlock.RenderTransform> 
  21.                     <CompositeTransform /> 
  22.                 </TextBlock.RenderTransform> 
  23.             </TextBlock> 
  24.  
  25.             <TextBlock Name="minuteHand"> 
  26.                 <TextBlock.RenderTransform> 
  27.                     <CompositeTransform /> 
  28.                 </TextBlock.RenderTransform> 
  29.             </TextBlock> 
  30.  
  31.             <TextBlock Name="secondHand"> 
  32.                 <TextBlock.RenderTransform> 
  33.                     <CompositeTransform /> 
  34.                 </TextBlock.RenderTransform> 
  35.             </TextBlock> 
  36.         </Grid> 
  37.     </Grid> 

 


  1. using System;  
  2. using System.Windows;  
  3. using System.Windows.Controls;  
  4. using System.Windows.Media;  
  5. using System.Windows.Threading;  
  6. using Microsoft.Phone.Controls;  
  7.  
  8. namespace HybridClock  
  9. {  
  10.     public partial class MainPage : PhoneApplicationPage  
  11.     {  
  12.         Point gridCenter;  
  13.         Size textSize;  
  14.         double scale;  
  15.  
  16.         public MainPage()  
  17.         {  
  18.             InitializeComponent();  
  19.  
  20.             DispatcherTimer tmr = new DispatcherTimer();//创建计时器  
  21.             tmr.Interval = TimeSpan.FromSeconds(1);//TimeSpan 结构  表示一个时间间隔。   
  22.             //DispatcherTimer.Interval 属性 获取或设置计时器刻度之间的时间段。  
  23.             //不能保证会正好在时间间隔发生时执行计时器,但能够保证不会在时间间隔发生之前执行计时器。   
  24.             //这是因为 DispatcherTimer 操作与其他操作一样被放置到 Dispatcher 队列中。   
  25.             //何时执行 DispatcherTimer 操作取决于队列中的其他作业及其优先级。  
  26.             tmr.Tick += OnTimerTick;//DispatcherTimer.Tick 事件 超过计时器间隔时发生。   
  27.             tmr.Start();//计时器开始  
  28.         }  
  29.  
  30.         //这个事件会在刚进入这个grid面板的时候触发  
  31.         //从而可以确定grid ContentPanel的准确高度和宽度  
  32.         void OnContentPanelSizeChanged(object sender, SizeChangedEventArgs args)//ContentPanel的ActualHeight 或 ActualWidth 属性的值发生更改时发生。  
  33.         {      
  34.             gridCenter = new Point(args.NewSize.Width / 2,  
  35.                                    args.NewSize.Height / 2);  
  36.  
  37.             textSize = new Size(referenceText.ActualWidth,  
  38.                                 referenceText.ActualHeight);//这个就是grid的内的高度和宽度  
  39.  
  40.             scale = Math.Min(gridCenter.X, gridCenter.Y) / textSize.Width;  
  41.  
  42.             UpdateClock();  
  43.         }  
  44.  
  45.         //计时器触发的事件函数  
  46.         void OnTimerTick(object sender, EventArgs e)  
  47.         {  
  48.             UpdateClock();  
  49.         }  
  50.  
  51.         void UpdateClock()  
  52.         {  
  53.             DateTime dt = DateTime.Now;  
  54.             double angle = 6 * dt.Second;  
  55.             SetupHand(secondHand, "--------------秒针 " + dt.Second, angle);  
  56.             angle = 6 * dt.Minute + angle / 60;  
  57.             SetupHand(minuteHand, "---------分针 " + dt.Minute, angle);  
  58.             angle = 30 * (dt.Hour % 12) + angle / 12;  
  59.             SetupHand(hourHand, "------时针 " + (((dt.Hour + 11) % 12) + 1), angle);  
  60.         }  
  61.  
  62.         void SetupHand(TextBlock txtblk, string text, double angle)  
  63.         {  
  64.             txtblk.Text = text;  
  65.             CompositeTransform xform = txtblk.RenderTransform as CompositeTransform;//获取txtblk的RenderTransform 属性 并转化为CompositeTransform  
  66.             xform.CenterX = textSize.Height / 2;//移动的中心点的 x 坐标  
  67.             xform.CenterY = textSize.Height / 2;//移动的中心点的 y 坐标  
  68.             xform.ScaleX = scale;//设置 x 轴的缩放比例  
  69.             xform.ScaleY = scale;//设置 y 轴的缩放比例  
  70.             xform.Rotation = angle - 90;//获取或设置顺时针旋转角度(以度为单位)  
  71.             xform.TranslateX = gridCenter.X - textSize.Height / 2;//设置沿 x 轴平移对象的距离  
  72.             xform.TranslateY = gridCenter.Y - textSize.Height / 2;//设置沿 y 轴平移对象的距离  
  73.         }  
  74.     }  

 


本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079162