Mubah Mohamed
Mubah Mohamed

Reputation: 17

ActivityIndicator on Xamarin Forms

How to put an ActivityIndicator on Xamarin Forms OnStart() function. I am check Network access on OnStart() function.

Upvotes: 1

Views: 317

Answers (3)

Sean Anderson
Sean Anderson

Reputation: 660

Bind the ActivityIndicator to a property in your BaseViewModel (IsBusy).

View

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

BaseViewModel (Inherited by all ViewModels)

private bool _isBusy;

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

Get yourself a good MVVM framework (Prism) and put the network check in the OnNavigatedTo method for your start page.

 public override void OnNavigatedTo(INavigationParameters parameters)
 {
      IsBusy = true;
      await CheckNetwork();
      IsBusy = false;
 }

Now you can paste that same ActivityIndicator snippet into any page (XAML) that is bound to a ViewModel inheriting BaseViewModel and it will just work when you set IsBusy.

Upvotes: 1

I'm using network checking in my projects too, please check this:

using Plugin.Connectivity;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace PetBellies.View
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NoConnection : ContentPage
    {
        private bool wasNotConn = false;

        public NoConnection()
        {
            InitializeComponent();

            CrossConnectivity.Current.ConnectivityChanged += async (sender, args) =>
            {
                if (CrossConnectivity.Current.IsConnected && !wasNotConn)
                {
                    wasNotConn = true;

                    await Navigation.PushModalAsync(new NavigationPage(new MainPage()));
                }
                else
                {
                    wasNotConn = false;
                }
            };
        }

        public NoConnection(bool isFromLogin)
        {
            CrossConnectivity.Current.ConnectivityChanged += async (sender, args) =>
            {
                if (CrossConnectivity.Current.IsConnected && !wasNotConn)
                {
                    wasNotConn = true;

                    var page = new LoginPage();

                    var navPage = new NavigationPage(page);

                    NavigationPage.SetHasNavigationBar(navPage, false);

                    await Navigation.PushModalAsync(navPage);
                }
                else
                {
                    wasNotConn = false;
                }
            };
        }
    }
}

https://github.com/officialdoniald/PetBellies/blob/master/PetBellies/PetBellies/View/NoConnection.xaml.cs

If the connection lost, the application navigate to this page and stay on this page while the connection is unavailable.

Upvotes: 0

askepott
askepott

Reputation: 289

Haven't used ActivityIndicator, but this nuget works great: Acr.UserDialogs.

After installing and adding the initialization part in the MainActivity or ios equivalent, just add the following code between resource intensive threads in either your code-behind file or viewmodel (mvvm):

This works for code-behind file:

protected override async void OnAppearing(object sender, EventArgs e)
            {
                base.ViewIsAppearing(sender, e);

                UserDialogs.Instance.ShowLoading();
                //do stuff here
                UserDialogs.Instance.HideLoading();
            }

This works for FreshMVVM framework:

protected override async void ViewIsAppearing(object sender, EventArgs e)
            {
                base.ViewIsAppearing(sender, e);

                UserDialogs.Instance.ShowLoading();
                //do stuff here
                UserDialogs.Instance.HideLoading();
            }

Upvotes: 0

Related Questions