Correct use of Activity Indicator iOS and Android

MiguelCervantesMiguelCervantes MXMember ✭✭✭
edited June 2015 in Xamarin.Forms

I'm using the next code for login in my app:

    private Command loginCommand;
    public const string LoginCommandPropertyName = "LoginCommand";
    public Command LoginCommand
    {
        get
        {
            return loginCommand ?? (loginCommand = new Command(async () =>
            {
                if (IsLoading)
                    return;
                IsLoading = true;
                await ExecuteLoginCommand();
                IsLoading = false;
            }));
        }
    }

    protected async Task ExecuteLoginCommand()
    {
        bool isLogin = false;
        await Task.Run(async () =>
        {
             isLogin = await UserAccount.doLogin(username, password); //doLogin is a public async static Task<bool>
        });
            if (isLogin)
            {
            var mainP = new NavigationPage(new RootPage());
            await navigation.PushModalAsync(mainP);
            }
    }

Activity indicator runs well on Android with the code above but on iOS is not working at all, I removed the Task.Run and executed the login function but now there is no Activity indicator running either on Android nor iOS, Is there a problem with the await/Task.Run or can someone please lend me a right way to use activity indicators?

Thanks in advance

Answers

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited June 2015

    why do you need to use Task.Run anyway?
    why can't you have doLogin return a Task instead?

  • MiguelCervantesMiguelCervantes MXMember ✭✭✭

    Thanks @AndreiNitescu for your reply!

    Actually doLogin is a Task

    What I did was changing to this:

                await Task.Run( () =>
                {
                    isLogin =  UserAccount.doLogin(username, password).Result;
                });
    

    But no activityIndicator is shown on iOS

    The ModelView (part of it) is:

            var indicator = new ActivityIndicator
            {
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                Color = Color.Silver,
            };
    
            indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsLoading");
    

    Then I add the indicator to a StackLayout, on Android it shows but not on iOS

  • MiguelCervantesMiguelCervantes MXMember ✭✭✭

    Never mind, I was setting IsVisibleProperty and isRunnungProperty to the same binding:

    indicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsLoading");
    indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsLoading");

    After changing both:

       await Task.Run(async () =>
        {
             isLogin = await UserAccount.doLogin(username, password); //doLogin is a public async static Task<bool>
        });
    

    To:
    await Task.Run( () =>
    {
    isLogin = UserAccount.doLogin(username, password).Result;
    });

    And:

    indicator.SetBinding(ActivityIndicator.IsVisibleProperty, "IsLoading");
    indicator.SetBinding(ActivityIndicator.IsRunningProperty, "IsLoading");

    It now works on Android and on iOS.

    Finally I like to mention that I feel the app si a little bit slower than with the first await/async/await approach. But now it works on both so I'll deal with that

Sign In or Register to comment.