Silverlight 2.0小技巧(3) Timmer

摘要:Silverlight 2.0小技巧(3) Timmer

.xmal:
 <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Loaded="StartTimer" x:Name="TxtTime" />
 </Grid>


.xmal.cs
 public void StartTimer(object o, RoutedEventArgs sender)
        {
            System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds (每秒)
            myDispatcherTimer.Tick += new EventHandler(Each_Tick);
            myDispatcherTimer.Start();
        }
        // Fires every 1000 miliseconds while the DispatcherTimer is active.
        public void Each_Tick(object o, EventArgs sender)
        {
            TxtTime.Text = "現在時間: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm ss"); 
        }


參考文章 http://blogs.msdn.com/silverlight_sdk/archive/2008/03/27/make-a-silverlight-timer-silverlight-2.aspx