是否可以在不使用System.Threading或System.Timers的情况下为Xamarin Forms构建自己的计时器?
我试图做到这一点,并使用下面的代码,但它似乎太快了,是否有另一种方法我可以采取?
它适用于.Net 4.5上的PCL项目。
public class TimerHelper
{
public class Timer
{
private int _waitTime;
public int WaitTime
{
get { return _waitTime; }
set { _waitTime = value; }
}
private bool _isRunning;
public bool IsRunning
{
get { return _isRunning; }
set { _isRunning = value; }
}
public event EventHandler Elapsed;
public virtual void OnTimerElapsed(int _autoSwipes, int _swipes)
{
if (Elapsed != null)
{
Elapsed(this, new EventArgs());
if (_autoSwipes < _swipes)
{
Stop();
Start(_autoSwipes, _swipes);
}
else
{
Task.WaitAll();//MoveNext);
_autoSwipes++;
}
}
}
public Timer(int waitTime)
{
WaitTime = waitTime;
}
public async Task Start(int _autoSwipes, int _swipes)
{
int seconds = 0;
IsRunning = true;
while (IsRunning)
{
if (seconds != 0 && seconds % WaitTime == 0)
{
OnTimerElapsed(_autoSwipes, _swipes);
}
seconds++;
}
}
public void Stop()
{
IsRunning = false;
}
}
}