在Xamarin.Forms使用Thread.sleep代码 [英] Using Thread.Sleep in Xamarin.Forms

查看:1382
本文介绍了在Xamarin.Forms使用Thread.sleep代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行以下

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 ,但是我还是

名称'主题'不存在于当前的上下文

The name 'Thread' does not exist in the current context.

这个网站表明, Thread.sleep代码 Xamarin.Forms使用。我有这样一个共享的项目,而不是PCL。

This site suggests that Thread.Sleep can be used in Xamarin.Forms. I have this in a shared project, not a PCL.

推荐答案

如果您想等待

异步:等待Task.Delay(10000);

同步: Task.Delay(10000).Wait();

但请尽量避免阻塞UI线程,以确保良好的用户体验,并保持你的应用程序响应。

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

在Xamarin.Forms使用Thread.sleep代码

有两种Xamarin.Forms模板:

There are two Xamarin.Forms templates:


  • Xamarin.Forms便携式类库

  • Xamarin.Forms Portable Class Library

由于该PCL子集的机制,你就没有机会获得 Thread.sleep代码

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

Xamarin.Forms共享项目

Xamarin.Forms Shared Project

这个模板中包含不支持不同的平台 Thread.sleep代码。窗户UWP,Xamarin.Android和Xamarin.iOS支持它的Windows Phone 8.1和Windows 8.1没有。如果您在卸载/删除2个项目,解决方案的基础之上。 (不要忘了使用的System.Threading;在App.cs)

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)

这篇关于在Xamarin.Forms使用Thread.sleep代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆