我有一个运行良好的计时器,我遇到的问题是当应用程序处于睡眠模式或最小化时或在我按下“后退”按钮时,要保持计时器运行,计时器只有在我停下来时才停止我已经完成的按钮。
/// <summary>
/// Starts the timer
/// </summary>
private void StartCommandaction()
{
CancellationTokenSource cts = _cancellationTokenSource; // safe copy
Device.StartTimer(TimeSpan.FromSeconds(1),() =>
{
if (cts.IsCancellationRequested)
{
return false;
}
else
{
Device.BeginInvokeonMainThread(() =>
{
var totalTimeInt = string.IsnullOrEmpty(TxtTotalTime.Value) ? 0 : int.Parse(TxtTotalTime.Value);
var totalSec = (int)TotalSeconds.TotalSeconds;
TimeSpan _TimeSpan = new TimeSpan(totalTimeInt,totalSec); //TimeSpan.FromSeconds(TotalSeconds.TotalSeconds);
LblTime = string.Format("{0:00}:{1:00}:{2:00}",_TimeSpan.Hours,_TimeSpan.Minutes,_TimeSpan.Seconds);
IsVisibleTimerLabel = true;
Count();
});
return true;
}
});
IsVisibleButtonStart = false;
IsVisibleButton = true;
}