Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么活动指示器不能在Xamarin.forms中工作?_C#_Xamarin_Xamarin.forms_Portable Class Library - Fatal编程技术网

C# 为什么活动指示器不能在Xamarin.forms中工作?

C# 为什么活动指示器不能在Xamarin.forms中工作?,c#,xamarin,xamarin.forms,portable-class-library,C#,Xamarin,Xamarin.forms,Portable Class Library,我试图显示ActivityIndicator在我试图更新数据库中的列字段时按下按钮后,它没有出现?有什么问题 以下是我的代码: ActivityIndicator ai = new ActivityIndicator() { HorizontalOptions = LayoutOptions.CenterAndExpand, Color = Color.Black };

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

以下是我的代码:

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有机会更新之前禁用指示器

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

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.beginInvokeMainThread()
在后台工作完成后将执行封送回主线程。这并不总是必要的,但这是一个很好的实践。

您是如何定义IsBusy的?它是Bindableproperty吗?ProcessToCheckOut是按钮。。我应该添加属性吗@StarterPack IsBusy是内置的。它用于打开和关闭状态栏活动图标,但您也可以在自己的代码中使用它,就像在这里完成一样嗨,我只想分享我在这里找到并回答的一个稍微不同的方法:太棒了!在这么多的搜索和遵循这么多复杂的代码中,一次又一次地重复,几乎没有变化,没有一个被点击。但是你如此甜美简单的代码为我创造了巨大的魔力,我已经奋斗了这么多个月了!非常感谢!!Device.BeginInvokeMainThread(()=>可能是必须的,因为没有指示器停止,导航语句在没有实际导航的情况下传递。但是使用Device.BeginInvokeMainThread(()=>实现,它工作顺利。指示器停止,页面导航按预期执行。