Why Activity Indicator Not working In Xamarin.forms?
Asked Answered
K

2

6

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?

On the following My Code:

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
                }
            };
Konstantine answered 15/3, 2016 at 10:3 Comment(3)
How did you define IsBusy? Is it a Bindableproperty?Ladle
ProcessToCheckOut is button .. should i add a property ?!Konstantine
@Ladle IsBusy is built-in. It is used to turn the statusbar activity icon on and off - but you can also use it in your own code like is being done hereLurleen
L
15

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.

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));
        });
    });
};

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.

Lurleen answered 16/3, 2016 at 0:18 Comment(2)
Hi, I just want to share a slightly different approach I found and answered here: https://mcmap.net/q/1632972/-triggering-activity-indicator-in-xamarin-formsParasol
Awesome! Out of so many search and following so many complicated codes, repeatedly again and again with few variations, not a single got clicked. But your so sweet simple code made the great magic for me which I was fighting since so many months! Thanks a ton!! Device.BeginInvokeOnMainThread(() => is probably must as without that the indicator stopped and navigation statement passed without actual navigation. But with the Device.BeginInvokeOnMainThread(() => implementation it worked smoothly. The Indicator stopped and navigation of page was executed as expected.Shrier
D
0

your endoint must be getasync or postasync with await

Davena answered 12/11, 2022 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.