我正在尝试使用以下代码(简化)在Xamarin Android中每秒刷新一次TextView(按钮点击后):
void OnClick_Start(object sender, EventArgs ea)
{
RunOnUiThread(() =>
{
txtTest.Text = "Starting";
Thread.Sleep(1000);
txtTest.Text = "1000";
Thread.Sleep(1000);
txtTest.Text = "2000";
Thread.Sleep(1000);
txtTest.Text = "3000";
Thread.Sleep(1000);
txtTest.Text = "4000";
});
}
但是我只在4秒后显示最后一个值“4000”,而不是中间值。
我还使用了以下代码,结果相同
void OnClick_Start(object sender, EventArgs ea)
{
new Thread(new ThreadStart(() =>
{
RunOnUiThread(() =>
{
txtTest.Text = "Starting";
Thread.Sleep(1000);
txtTest.Text = "1000";
Thread.Sleep(1000);
txtTest.Text = "2000";
Thread.Sleep(1000);
txtTest.Text = "3000";
Thread.Sleep(1000);
txtTest.Text = "4000";
});
})).Start();
}
如何获得我想要的结果?
答案 0 :(得分:2)
你可以在其他线程上等待一秒钟 - 非UI。
例如:
UMAT