tes*_*ing 13 c# sleep xamarin.ios xamarin 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;
})),
},
}
}
};
Run Code Online (Sandbox Code Playgroud)
我包括System.Threading和System.Threading.Tasks,但我仍然得到
"Thread"这个名称在当前上下文中不存在.
该网站建议Thread.Sleep可以使用Xamarin.Forms.我在共享项目中有这个,而不是PCL.
Sve*_*übe 30
如果你想等
异步: await Task.Delay(10000);
同步: Task.Delay(10000).Wait();
但请尽量避免阻止UI线程以确保良好的用户体验并保持应用程序的响应.
在Xamarin.Forms中使用Thread.Sleep
有两个Xamarin.Forms模板:
Xamarin.Forms可移植类库
由于PCL子集机制,您没有机会获得Thread.Sleep.
2017年更新: PCL现已弃用.如果您使用.netstandard 2.0,您可以Thread.Sleep按照习惯使用它.
Xamarin.Forms共享项目
此模板包含不支持的不同平台Thread.Sleep.Windows UWP,Xamarin.Android和Xamarin.iOS不支持它,Windows Phone 8.1和Windows 8.1.如果卸载/删除2个项目,则构建解决方案.(不要忘记使用System.Threading;在你的App.cs中)