使用了Thread.Sleep我想執行以下在Xamarin.Forms
MainPage = new ContentPage
{
Content = new StackLayout
{
Children =
{
new Button
{
Text = "Thread.Sleep",
Command = new Command(() =>
{
Thread.Sleep(1000);
MainPage.Animate("", x => MainPage.BackgroundColor = Color.FromRgb(x, x, x));
}),
},
new Button
{
Text = "Task.Run + Thread.Sleep",
Command = new Command(async() =>
{
await Task.Run(() => Thread.Sleep(1000));
MainPage.Animate("", x => MainPage.BackgroundColor = Color.FromRgb(x, x, x));
})
},
new Button
{
Text = "Device.StartTimer",
Command = new Command(() => Device.StartTimer(
TimeSpan.FromSeconds(1),
() =>
{
MainPage.Animate("", x => MainPage.BackgroundColor = Color.FromRgb(x, x, x));
return false;
})),
},
}
}
};
我包括System.Threading
和System.Threading.Tasks
,但我仍然得到
名稱「主題」不存在的當前上下文。
This site建議Thread.Sleep
可用於Xamarin.Forms
。我在一個共享的項目中,而不是PCL。
'Thread.Sleep'應同時使用幾乎所有的UI框架來避免(的WinForms/WPF/Xamarin.Forms/Silverlight的/ ... ),因爲它會凍結UI線程並掛起UI。 –
它僅用於演示目的,但謝謝您的補充。 – testing