Using Thread.Sleep in Xamarin.Forms

后端 未结 2 1847
臣服心动
臣服心动 2020-12-31 06:55

I want to execute the following

MainPage = new ContentPage
{
    Content = new StackLayout
    {
        Children =
        {
            new Button
                 


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 07:21

    If you want to wait

    asynchronously: await Task.Delay(10000);

    synchronously: Task.Delay(10000).Wait();

    But please try to avoid blocking the UI thread to ensure a good user experience and keep your app responsive.

    Using Thread.Sleep in Xamarin.Forms

    There are two Xamarin.Forms templates:

    • Xamarin.Forms Portable Class Library

      Because of the PCL subset mechanism, you have no chance to get Thread.Sleep.

      Update 2017: PCLs are deprecated, now. If you use .netstandard 2.0, you can use Thread.Sleep as you are used to it.

    • Xamarin.Forms Shared Project

      This Template contains different platforms that do not support Thread.Sleep. Windows UWP, Xamarin.Android and Xamarin.iOS support it, Windows Phone 8.1, and Windows 8.1 not. If you unload/delete the 2 projects, the solution builds. (don't forget using System.Threading; in your App.cs)

提交回复
热议问题