Silverlight如何:建立計時器

來源:互聯網
上載者:User

若要在 Silverlight 中建立計時器,請使用 System.Windows.Threading 命 名空間中的 DispatcherTimer 類。

下面的樣本示範一個使用 DispatcherTimer 的簡單計數器。

運行此樣本:http://go.microsoft.com/fwlink/? LinkId=139798&sref=timer

樣本

XAML

<Grid x:Name="LayoutRoot" Background="White">

  <!-- Just a TextBlock to show the output of the timer.  -->
  <TextBlock Loaded="StartTimer" x:Name="myTextBlock" />
</Grid>

VB

Public Sub StartTimer(ByVal o As Object, ByVal sender  As RoutedEventArgs)
         Dim myDispatcherTimer As  System.Windows.Threading.DispatcherTimer = New  System.Windows.Threading.DispatcherTimer
         myDispatcherTimer.Interval = New TimeSpan(0, 0,  0, 0, 100)
         ' 100 Milliseconds 
         AddHandler myDispatcherTimer.Tick, AddressOf  Me.Each_Tick
         myDispatcherTimer.Start
     End Sub
     ' A variable to count with.
     Private i As Integer = 0

     ' Raised every 100 miliseconds while the  DispatcherTimer is active.
     Public Sub Each_Tick(ByVal o As Object, ByVal sender  As EventArgs)
         ("Count up: " + i) = (("Count up: " + i)   _
                     + 1)
         myTextBlock.Text = ("Count up: " + i)
         ToString
     End Sub

C#

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, 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();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.