.NET Micro Framework版的WPF创建的计时器
1 using System; 2 3 using Microsoft.SPOT; 4 using Microsoft.SPOT.Input; 5 using Microsoft.SPOT.Presentation; 6 using Microsoft.SPOT.Presentation.Controls; 7 using System.Threading; 8 9 namespace TimeDisplay 10 { 11 public class Program : Microsoft.SPOT.Application 12 { 13 public static void Main() 14 { 15 Program myApplication = new Program(); 16 17 Window mainWindow = myApplication.CreateWindow(); 18 19 // 创建一个 GPIO buttons 20 GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null); 21 22 // 启动程序 23 myApplication.Run(mainWindow); 24 } 25 26 private Window mainWindow; 27 28 Text text = new Text(); 29 public Window CreateWindow() 30 { 31 // 创建一个Window窗体 32 mainWindow = new Window(); 33 mainWindow.Height = SystemMetrics.ScreenHeight; 34 mainWindow.Width = SystemMetrics.ScreenWidth; 35 36 //创建计时器 37 timer = new DispatcherTimer(); 38 timer.Interval = new TimeSpan(0, 0, 1);//间隔1秒 39 timer.Tick += new EventHandler(timer_Tick); 40 timer.Start(); 41 42 // 创建text控件 43 //Text text = new Text(); 44 45 text.Font = Resources.GetFont(Resources.FontResources.small); 46 //text.TextContent = Resources.GetString(Resources.StringResources.String1); 47 text.HorizontalAlignment = Microsoft.SPOT.Presentation.HorizontalAlignment.Center; 48 text.VerticalAlignment = Microsoft.SPOT.Presentation.VerticalAlignment.Center; 49 50 // 将text增加到window 51 mainWindow.Child = text; 52 53 // Connect the button handler to all of the buttons. 54 mainWindow.AddHandler(Buttons.ButtonUpEvent, new RoutedEventHandler(OnButtonUp), false); 55 56 // 设置窗体是否可见 57 mainWindow.Visibility = Visibility.Visible; 58 59 // 增加Button焦点 60 Buttons.Focus(mainWindow); 61 62 return mainWindow; 63 } 64 65 DispatcherTimer timer = new DispatcherTimer(); 66 private void OnButtonUp(object sender, RoutedEventArgs evt) 67 { 68 ButtonEventArgs e = (ButtonEventArgs)evt; 69 70 // 输出调试结果 71 Debug.Print(e.Button.ToString()); 72 } 73 74 void timer_Tick(object sender, EventArgs e) 75 { 76 77 text.TextContent = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ddd"); 78 79 } 80 } 81 }
第一次用.NET Micro Framework编写了定时器,使用DispatchTimer类代替Timer类,这样可以避免一些潜在的问题

程序不难,类似于用Timer类来获取系统时间,显示的话需要把text.TextContent加到timer_Tick中

浙公网安备 33010602011771号