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

ActivityIndicator issue xamarin form android application

$
0
0

I have a login page, i want to show the ActivityIndicator while the page is busy executing an async method.

the ActivityIndicator is showing based on the IsLoading property on the ViewModel class. implemented the INotifyPropertyChanged in my ViewModel class but still not showing the ActivityIndicator , this is my code:

        <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">

                    <StackLayout AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">

                        <Entry x:Name="username" Placeholder="Email" Text="ofern@hotmail.com"></Entry>
                        <Entry x:Name="password" Placeholder="Password" Text="Osm$test01" IsPassword="True"></Entry>
                        <Button x:Name="btnLogin" Text="Login" Clicked="btnLogin_Clicked"></Button>

                    </StackLayout>

                    <StackLayout
                    Padding="12"
                         AbsoluteLayout.LayoutFlags="PositionProportional"
                         AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1">

                        <ActivityIndicator x:Name="indicator" IsRunning="{Binding IsLoading}" IsVisible="{Binding IsLoading}"  Color ="#80000000"/>

                    </StackLayout>

                </AbsoluteLayout>

    public Login ()
            {
                InitializeComponent ();
                BindingContext = new ViewModels.LoginViewModel();
            }

    private async void btnLogin_Clicked(object sender, EventArgs e)
            {
                try
                {
                    ((ViewModels.LoginViewModel)BindingContext).IsLoading = true;

                    if (string.IsNullOrWhiteSpace(username.Text))
                        throw new System.ComponentModel.WarningException("Please enter email.");

                    if (string.IsNullOrWhiteSpace(password.Text))
                        throw new System.ComponentModel.WarningException("Please enter password.");

                    var user = new Ascendant.Utils.Security().Authenticate(username.Text, password.Text);
                    if (user == null)
                        throw new System.ComponentModel.WarningException($"Unable to find the user with the email {username.Text}");

                    if (user.IsInsured)
                    {
                        throw new System.ComponentModel.WarningException("Insured Portal: Pending Development");
                    }
                    else if (user.IsAgent)
                    {
                        await Navigation.PushModalAsync(new Ascendant.Views.Agent.AgentMainPage());
                    }
                    else
                    {
                        throw new System.ComponentModel.WarningException("User is not valid.");
                    }
                }
                catch (System.ComponentModel.WarningException ex)
                {
                    await DisplayAlert("Please Review", ex.Message, "OK");
                }
                catch (Exception ex)
                {
                    await Utils.Util.ReportErrorAsync(ex);
                    await DisplayAlert("Process Failed", "There was an issue with your request", "OK");
                }
                finally
                {
                    ((ViewModels.LoginViewModel)BindingContext).IsLoading = false;
                }
            }


    public class LoginViewModel: INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            private bool _IsLoading;
            public bool IsLoading
            {

                get
                {
                    return _IsLoading;
                }

                set
                {
                    _IsLoading = value;
                    NotifyPropertyChanged("IsLoading");
                }
            }

            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



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