Windows Phone开发中时钟案例解析


  本文标签:WindowsPhone开发

  本文和大家重点讨论一下Windows Phone开发中一个时钟的例子,Expressblend工具是开发silverlightUi的重要工具,在这里有一个从silverlight移植过来的时钟小例子可以看出在Phone7上这个工具一样也很重要并且可以提高开发效率  。

  Windows Phone开发中一个时钟的例子

  Expressblend工具是开发silverlightUi的重要工具,在这里有一个从silverlight移植过来的时钟小例子可以看出Windows Phone开发中在Phone7上这个工具一样也很重要并且可以提高开发效率  。

  一.在blend工具中,可以用Ellipse绘制表盘,通过在property中使用渐变色来产生立体效果  。还可以能过gradient工具来调整渐变色  。

  

 

 

 

 

 

  二.用Ellipse工具绘制表针轴,并设置圆的strokethickness来改变线的粗细  。

  

 

  三.用Rectangle工具来绘制三个表针,并放好位置  。

  

 

  四.增加动画效果

  1. <Storyboardx:NameStoryboardx:Name="clockStoryboard"> 
  2.  
  3. <DoubleAnimationx:NameDoubleAnimationx:Name="hourAnimation" 
  4. Storyboard.TargetName="HourHandTransform" 
  5. Storyboard.TargetProperty="Angle" 
  6. Duration="12:0:0"RepeatBehavior="Forever"To="360"/> 
  7.  
  8.  
  9. <DoubleAnimationx:NameDoubleAnimationx:Name="minuteAnimation" 
  10. Storyboard.TargetName="MinuteHandTransform" 
  11. Storyboard.TargetProperty="Angle" 
  12. Duration="1:0:0"RepeatBehavior="Forever"To="360"/> 
  13.  
  14.  
  15. <DoubleAnimationx:NameDoubleAnimationx:Name="secondAnimation" 
  16. Storyboard.TargetName="SecondHandTransform" 
  17. Storyboard.TargetProperty="Angle" 
  18. Duration="0:1:0"RepeatBehavior="Forever"To="360"/> 
  19. Storyboard> 
  20.  

  

  Windows Phone开发中这时运行一下此程序,已经可以看到时钟的表针在走了  。

  五.控制表针行为语句

  1. voidMainPage_Loaded(objectsender,RoutedEventArgse)  
  2. {  
  3. //Thecurrentdateandtime.  
  4. System.DateTimecurrentDate=DateTime.Now;  
  5.  
  6. //Findtheappropriateangle(indegrees)forthehourhand  
  7. //basedonthecurrenttime.  
  8. doublehourangle=(((float)currentDate.Hour)/12)*360+currentDate.Minute/2;  
  9.  
  10. //Thesameasfortheminuteangle.  
  11. doubleminangle=(((float)currentDate.Minute)/60)*360;  
  12.  
  13. //Thesameforthesecondangle.  
  14. doublesecangle=(((float)currentDate.Second)/60)*360;  
  15.  
  16. //Setthebeginningoftheanimation(Fromproperty)totheangle  
  17. //correspongingtothecurrenttime.  
  18. hourAnimation.From=hourangle;  
  19.  
  20. //Settheendoftheanimation(Toproperty)totheangle  
  21. //correspondingtothecurrenttimePLUS360degrees.Thus,the  
  22. //animationwillendaftertheclockhandmovesaroundtheclock  
  23. //once.Note:TheRepeatBehaviorpropertyoftheanimationisset  
  24. //to"orever"sotheanimationwillbeginagainassoonasitcompletes.  
  25. hourAnimation.To=hourangle+360;  
  26.  
  27. //Sameaswiththehouranimation.  
  28. minuteAnimation.From=minangle;  
  29. minuteAnimation.To=minangle+360;  
  30.  
  31. //Sameaswiththehouranimation.  
  32. secondAnimation.From=secangle;  
  33. secondAnimation.To=secangle+360;  
  34.  
  35. this.clockStoryboard.Begin();  
  36. }  
  37.  

  

  源代码:

  /Files/randylee/MyClock.rar