为什么活动指示器在Xamarin.forms中不起作用? [英] Why Activity Indicator Not working In Xamarin.forms?

查看:75
本文介绍了为什么活动指示器在Xamarin.forms中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示ActivityIndicator,当我尝试更新数据库上的列字段时,按下按钮后,它没有出现吗?有什么问题吗?

I am trying to display ActivityIndicator After I press a button while I am trying to update The Column Field On DataBase, it is not appearing? What is the problem?

在以下我的代码"上:

ActivityIndicator ai = new ActivityIndicator()
            {
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                Color = Color.Black
            };
            ai.IsRunning = true;
            ai.IsEnabled = true;
            ai.BindingContext = this;
            ai.SetBinding(ActivityIndicator.IsVisibleProperty, "IsBusy");

            ProcessToCheckOut = new Button { Text = "Set Inf" };
            ProcessToCheckOut.Clicked += (object sender, EventArgs e) =>
            {
                this.IsBusy = true;
                UserUpdateRequest user=new UserUpdateRequest();
                user.userId = CustomersPage.ID;
                appClient.UpdateInfo(user);                  
                this.IsBusy = false;
                Navigation.PushAsync(new CheckoutShippingAddressPage(appClient));
            };
         Content = new StackLayout
            {
                Children={
                tb,
                ai,
                ProcessToCheckOut
                }
            };

推荐答案

this.IsBusy=true;this.IsBusy=false;之间的代码都不是异步的.因此,发生的事情是先启用指示器,然后继续在主线程上进行工作,然后在UI有机会更新之前禁用指示器.

None of the code between this.IsBusy=true; and this.IsBusy=false; is asynchronous. So what is happening is that you enable the indicator but then continue to do work on the main thread and then disable the indicator before the UI has a chance to update.

要解决此问题,您需要将appClient.UpdateInfo(user)放入异步代码块(以及PushAsync和禁用活动指示器以及其他一些代码).如果您没有异步版本的UpdateInfo(),则可以将其推送到后台线程中...假设它所做的任何工作实际上都可以在后台线程中运行.

To fix this, you would need to put appClient.UpdateInfo(user) into an async code block (along with the PushAsync and disabling of activity indicator and probably some of the other code). If you don't have an async version of UpdateInfo() then you can push it off into a background thread... assuming whatever work it does is actually safe for running in a background thread.

ProcessToCheckOut.Clicked += (object sender, EventArgs e) =>
{
    this.IsBusy = true;
    var id = CustomersPage.ID;
    Task.Run(() => {
        UserUpdateRequest user=new UserUpdateRequest();
        user.userId = id;
        appClient.UpdateInfo(user);
        Device.BeginInvokeOnMainThread(() => {
            this.IsBusy = false;
            Navigation.PushAsync(new CheckoutShippingAddressPage(appClient));
        });
    });
};

请注意,一旦后台工作完成,我还使用Device.BeginInvokeOnMainThread()将执行封送回主线程.这并非总是必要的,但这是一种好习惯.

Note that I also used Device.BeginInvokeOnMainThread() to marshal execution back to the main thread once the background work is done. This isn't always necessary but it is good practice.

这篇关于为什么活动指示器在Xamarin.forms中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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