So I have two buttons Start/Stop and start works fine because it starts at the beginning each time start is clicked, which is what I want. But I'm new to xamarin forms and don't entirely understand how to stop device.starttimer.
This is what I have currently and it's not working. (don't worry about the sound stuff)
//timer
bool shouldRun = false;
private void timer()
{
Device.StartTimer(TimeSpan.FromSeconds(3), () =>
{
// Do something
label.Text = "Time is up!";
//shows start button instead of stop button
startButton.IsVisible = true;
//hides stop button
stopButton.IsVisible = false;
return shouldRun;
});
}
private void STOPButton_Clicked(object sender, EventArgs e)
{
//shows start button instead of stop button
startButton.IsVisible = true;
//hides stop button
stopButton.IsVisible = false;
//stops timer
shouldRun = false;
//stops sound
}
private void STARTButton_Clicked(object sender, EventArgs e)
{
//hides start button
startButton.IsVisible = false;
//shows stop button instead of start button
stopButton.IsVisible = true;
//starts timer from beginning
timer();
//starts sound from beginning
}
falsefrom the Func<bool> the timer is stopped.shouldRun = falsein the stop button, the timer delegate will run one more time at the end of its 3 second timer, if you do not want the label being set to "Time is up!", then you would need to perform a check ofshouldRunbefore that and jump over that assignment (or perform an early exit,return false).