SharePointAds TextOnly

Thursday 11 February 2010

Silverlight: How to use Timer Control?

MainPage.xmal:



In between control add following code,

<Grid x:Name="LayoutRoot">

<TextBlock Loaded="StartTimer" x:Name="myTextBlock" />

Grid>

Right click on "StartTimer" and select 'Navigate to Event Handler', it will create new event handler method in .cs file. Add following code in xmal.cs file:



MainPage.xmal.cs:

private void StartTimer(object sender, RoutedEventArgs e)

{

System.Windows.Threading.DispatcherTimer newDispatcherTimer = new System.Windows.Threading.DispatcherTimer();

newDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // for 1 seconds

newDispatcherTimer.Tick += new EventHandler(Every_Seconds);

newDispatcherTimer.Start();

}

int iCounter = 0;

// Retrive after every 1000 millseconds(1seconds) while the DispatcherTimer is active.

public void Every_Seconds(object o, EventArgs sender)

{

myTextBlock.Text = "Second Counter : " + (iCounter++).ToString();

}



Hope this will clear timer concepts in Silverlight!!!







No comments:

Post a Comment