如何让活动指示器等待函数 [英] How to make activity indicator wait for a function

查看:28
本文介绍了如何让活动指示器等待函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用活动指示器来显示我的函数正在加载.它运行得如此之快,我可以看到我的活动指示器,但该功能尚未完成加载

I would like to use activity indicator to show that my function is loading. It goes so fast that I can see my activity inndicator but the function have not finished loading

问题:如何在功能真正完成运行时使我的活动指示器为假

这是我的代码:

  public MainPage()
        {


            InitializeComponent();
            On<iOS>().SetUseSafeArea(true);

            activityIndicator.IsRunning = true;
         
            MyPageName = "home";


            var tasks = new List<Task>();

          
            tasks.Add(Task.Run(async () => {

            GetMyLeafLanguage(); //p


            }));


            tasks.Add(Task.Run(async () => {

                    if (App.CheckConnection() == true)
                    {
                        MyAds();  // doit être apres l'initialisation         //
                        LoadtestPage();
                    }
            }));



            Task.WhenAll(tasks);
            #endregion My Initial Tasks

             OnGoTutorial();


            MyNotificationActivate();

            activityIndicator.IsRunning = false;


        }

推荐答案

按照您编写的方式,任务必须在页面可以显示之前完成其工作.所以页面(终于)显示出来了,但是由于工作已经完成,活动指示器立即消失了.

The way you wrote it, the tasks had to finish their work, before the page could display. So the page (finally) displayed, but since the work was already done, the activity indicator immediately disappeared.

这些功能应该在页面显示后运行.

否则,当任务运行时,用户将看到一个空白屏幕(或在您开始创建此页面之前显示的任何页面).

Otherwise, while the tasks run, user will be looking at a blank screen (or at whatever page was showing before you starting creating this page).

遗憾的是,没有在页面首次显示后运行的跨平台事件.

Unfortunately, there is no cross-platform event that runs after a page displays for the first time.

这是在后台工作完成时让页面显示的一种方法:

Here is one way to let the page display while the background work is being done:

public partial class MyPage : ...
{
    protected override void OnAppearing()
    {
        base.OnAppearing();

        BeginWorkWithIndicator();
    }
    
    private void BeginWorkWithIndicator()
    {
        activityIndicator.IsRunning = true;

        // The delay gives Xamarin UI a little time, before your background work begins.
        // Without this delay, under some circumstances, the page might not show up as quickly.
        Device.StartTimer(TimeSpan.FromMilliseconds(100), () => {
            OneTimeBackgroundWork();
            // Run timer only once.
            return false;
        });
    }

    private void OneTimeBackgroundWork()
    {
        ... Long-running work - MUST NOT TOUCH UI ...
        
        // When done.
        Device.BeginInvokeOnMainThread(() => {
            // NOW MAKE UI CHANGES, based on variables set by work above.
            ...
            activityIndicator.IsRunning = false;
        });
    }
}

这篇关于如何让活动指示器等待函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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