Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Activity Indicator is not Showing unless async method finish

$
0
0

I have a login page that takes token and approve from webAPI. I need to show activity Indicator on that period. I am using MVVM pattern.

My XAML ;

  <StackLayout>
  <Grid>
 <Grid>
 <Button x:Name="Btn_Login" TextColor="White" Text="LOGIN" Command="{Binding LoginCommand}" BackgroundColor="Green" Grid.Row="0" Grid.Column="2" CornerRadius="25" BorderRadius="25"/>
            </Grid>

        </Grid>

    </StackLayout>
    <StackLayout>
        <ActivityIndicator IsVisible="{Binding IsBusy}"  IsRunning="{Binding IsBusy}" Color ="Black"/>
    </StackLayout>

My MVVM;

 private bool _isBusy;
    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;
            OnPropertyChanged(nameof(IsBusy));
        }
    }

  public ICommand LoginCommand => new AsyncCommand(Login);

  private async Task Login()
    {

        await MakeTrueIsBusyStatus();
        await AddValidations();

        bool isValid = Validate();
        if (isValid)
        {
            User user = new User(LoginUserName.Value, LoginPassword.Value);


            var token = "";
            ServiceManager serviceManager = new ServiceManager(user);

            string response = await serviceManager.Login();

            var apiResponse = JsonConvert.DeserializeObject<WebApiResponse<MyResponse>>(response);
            var jObjectResponse = JObject.Parse(response);
            var statusCode = jObjectResponse.GetValue("statusCode").ToString();

            if (apiResponse.StatusCode == (Int32)HttpStatusCode.OK)
            {
                var jObjectResult1 = JObject.Parse(jObjectResponse.GetValue("result").ToString());

                token = jObjectResult1.GetValue("token").ToString();
                try
                {
                    await Navigation.PushModalAsync(new MyMasterDetailPage());

                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                await MakeFalseIsBusyStatus();
                var errorResponse = JObject.Parse(jObjectResponse.GetValue("errorResponse").ToString());
                await Application.Current.MainPage.DisplayAlert(jObjectResponse.GetValue("message").ToString(),
                    errorResponse.GetValue("exceptionType").ToString(), "cancel");

            }
        }

     private async Task MakeTrueIsBusyStatus()
    {
        IsBusy = true;
    }

    private async Task MakeFalseIsBusyStatus()
    {
        IsBusy = false;
    }

My problem is that when i click login button my UI froze and not showing activity indicator unless login method finish. I used all method async but still UI freeze so activity indicator not showing. Please help why is this happening?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>