[英]ViewDidAppear is causing my label to dissapear (Xamarin.ios, C#)
i want to segue to a new View Controller when the countdown timer, here set to 10 seconds, reaches 0 seconds. 我想在倒数计时器(此处设置为10秒)达到0秒时选择到新的View Controller。 it does this using the thread logic below.
它使用下面的线程逻辑来做到这一点。 the label usually shows the countdown "10, 9, 8, 7" but since i used ViewDidAppear, it doesnt show.
该标签通常显示倒数“ 10、9、8、7”,但是由于我使用的是ViewDidAppear,因此不会显示。 at the end itll flash 0 seconds, and the segue will take place.
最后,它会闪烁0秒钟,然后开始计时。 i need the countdown to show the whole time, and cant figure out how and why its dissapearing
我需要倒计时以显示整个时间,并且无法弄清楚其消失的方式和原因
using System.Timers; 使用System.Timers; using System.Threading;
使用System.Threading;
... private System.Timers.Timer mytimer; ...私人System.Timers.Timer mytimer; private int countSeconds;
private int countSeconds;
... ...
public override void ViewDidLoad()
{
base.ViewDidLoad();
mytimer = new System.Timers.Timer();
//Trigger event every second
mytimer.Interval = 1000; //1000 = 1 second
mytimer.Elapsed += OnTimedEvent;
countSeconds = 10; // 300 seconds
mytimer.Enabled = true;
mytimer.Start();
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
countSeconds--;
int seconds = countSeconds % 60;
int minutes = countSeconds / 60;
string DHCountdownTime = (countSeconds / 60).ToString() + ":" + (countSeconds % 60).ToString("00"); //to give leading 0. so 9 seconds isnt :9 but :09
InvokeOnMainThread(() =>
{
lblTimer.Text = DHCountdownTime;
});
if (countSeconds == 0)
{
mytimer.Stop();
}
}
...
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
Thread.Sleep(countSeconds * 1000);
PerformSegue("DHSegue", this);
... ...
Your Thread.Sleep
is blocking the UI thread: 您的
Thread.Sleep
正在阻止UI线程:
Thread.Sleep(countSeconds * 1000);
Use a task (or another thread) in order allow the UI thread to continue to process messages: 使用任务(或另一个线程)以使UI线程继续处理消息:
await Task.Delay(countSeconds * 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.