Silverlight
若要在 Silverlight 中创建计时器,请使用 System.Windows.Threading 命名空间中的 DispatcherTimer 类。
下面的示例演示一个使用 DispatcherTimer 的简单计数器。
<Grid x:Name="LayoutRoot" Background="White"> <!-- Just a TextBlock to show the output of the timer. --> <TextBlock Loaded="StartTimer" x:Name="myTextBlock" /> </Grid>
public void StartTimer(object o,RoutedEventArgs sender) { System.Windows.Threading.dispatcherTimer mydispatcherTimer = new System.Windows.Threading.dispatcherTimer(); mydispatcherTimer.Interval = new TimeSpan(0,100); // 100 Milliseconds mydispatcherTimer.Tick += new EventHandler(Each_Tick); mydispatcherTimer.Start(); } // A variable to count with. int i = 0; // Raised every 100 miliseconds while the dispatcherTimer is active. public void Each_Tick(object o,EventArgs sender) { myTextBlock.Text = "Count up: " + i++.ToString(); }