停止C#中的系统计时器



我使用的是System.Timers,计时器工作得很好,例如在Second 4, 8, 12, 16上拍照。

但是,我希望它在Second 17上导航,但它不会。

代码

public partial class CustomCameraPage : ContentPage
{
private static Timer timer_click;
int Seconds = 0;
public CustomCameraPage()
{
timer_click = new Timer();
timer_click.Interval = 1000;
timer_click.Elapsed += OnTimedEvent;
timer_click.Enabled = true;
timer_click.AutoReset = true;
timer_click.Start();
InitializeComponent();
}

public void OnTimedEvent(object source, ElapsedEventArgs e)
{
Seconds++;
if (Seconds == 4 || Seconds == 8 || Seconds == 12 || Seconds == 16) MessagingCenter.Send<object>(this, "A");
if (Seconds == 17) Navigation.PushModalAsync(new FormPage());
}   
}

在MainThread 上执行导航

if (Seconds == 17) 
{
timer_click.Stop();
MainThread.BeginInvokeOnMainThread(() =>
{
Navigation.PushModalAsync(new FormPage());
});
}

最新更新