Search This Blog

Sunday 16 September 2018

Xamarin Forms Timer

Xamarin Form Timer:

Xamarin Form Timer will execute the block of code in specified time interval.


 

// Here is the example that on the click of  Start Button The timer will execute the block of code for every second and update the Label Text & On the Click of Stop Button Timer will stop.

private void btnStartTimer_Clicked(object sender, EventArgs e)
{
    if (btnStartTimer.Text == "Start")
    {
        returnValue = true;
        btnStartTimer.Text = "Stop";
    }
    else
    {
        btnStartTimer.Text="Start";
        returnValue = false;
    }
    int count = 0;
    lblTimer.Text = "";
    Device.StartTimer(TimeSpan.FromSeconds(1), () =>
    {
        // Do something
        count += 1;
        Device.BeginInvokeOnMainThread(() =>
        {
            lblTimer.Text = count.ToString() ;
        });
            return returnValue; // True = Repeat again, False = Stop the timer
    });
}

No comments:

Post a Comment

Popular Posts