Showing Activity Indicator in Page Model Load

KathirVelKathirVel USMember ✭✭

Hi All,

We are working with Fresh MVVM Architecture on Xamarin Forms Development. In our application, we are facing issue for Showing/Hiding the Activity Indicator in Page Model Loading while making WCF REST Service Calls.

Kindly suggest us on showing the activity Indicator Properly while loading the Page Model Class (As we make 5 to 6 WCF REST Service Calls in Loading).

Please find below the Sample Code for reference:

Content Page:

<ContentPage.Content>
  <Grid>
    <Grid x:Name="LayoutRoot_Loading" VerticalOptions="Center" HorizontalOptions="Center" >
      <ActivityIndicator IsRunning="{Binding ActIndRunning,Mode=TwoWay}" IsVisible="{Binding ActIndRunning,Mode=TwoWay}" 
                        Color="White"  VerticalOptions="Center" HorizontalOptions="Center" x:Name="ActIndicator_Loading" />
    </Grid>
    <Grid x:Name="LayoutRoot" BackgroundColor="White" Opacity="{Binding LayoutRootOpacity,Mode=TwoWay}" IsEnabled="{Binding LayoutIsEnabled,Mode=TwoWay}" >
      <Grid.RowDefinitions >
        <RowDefinition Height="9.4*"/>
        <RowDefinition Height=".6*"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>      
      <Label Grid.Row="0" Grid.Column="0" Text="Application Server URL:" HorizontalOptions="Fill" VerticalOptions="CenterAndExpand" TextColor="#888888"/>  
      <StackLayout Grid.Row="1" Grid.Column="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Orientation="Vertical" BackgroundColor="#DAE3EA">
        <!-- few label, entry , extended picker and listview controls are created below and binded -->
      </StackLayout>      
    </Grid>
  </Grid>
  </ContentPage.Content>

FreshMVVM Page Model:

    [ImplementPropertyChanged]
    public class NavigationNextPageModel : FreshBasePageModel
    {

        List<Helpdesk> List_Equipment, List_Sites;
        public NavigationNextPageModel( )
        {
            try
            {
                /* We have tried to load the service calls here but no */
                //LoadWCFServiceFunctionsForControlBinding();
            }
            catch (Exception ex) { CoreMethods.DisplayAlert("SampleApp", "Exception", "Ok"); }
        }

        public override void Init(object initData)
        {
            LoadWCFServiceFunctionsForControlBinding();
        }

        async void LoadWCFServiceFunctionsForControlBinding()
        {
            ShowProgressDialog(true);

            // When we set the task Delay it shows the Activity Indicator properly for loading. 
            //await Task.Delay(5000);

            //When we make WCF REST Service Callls its not showing the activity Indicator and loads the page .  
            object[] Parameters = new object[1];
            Parameters[0] = "SampleData";
            List_Sites =await App.SampleWCFManager.FillUserSitesAsync(Parameters);
            List_Equipment = await App.SampleWCFManager.FillUserEquipmentAsync();
            Parameters = new object[1];
            Parameters[0] = "SiteName";
            List_Staffs = await App.SampleWCFManager.FillUsersAsync(Parameters);

            ShowProgressDialog(false);
        }

        protected override void ViewIsAppearing(object sender, System.EventArgs e)
        {
            base.ViewIsAppearing(sender, e);
            //LoadWCFServiceFunctionsForControlBinding();
        }

        //This method is called when the view is disappearing.
        protected override void ViewIsDisappearing(object sender, System.EventArgs e)
        {
            base.ViewIsDisappearing(sender, e);
        }

        public override void ReverseInit(object value)
        {

        }

        public bool _ActIndRunning { get; set; }
        public bool ActIndRunning
        {
            set
            {
                if (_ActIndRunning != value)
                {
                    _ActIndRunning = value;
                    RaisePropertyChanged("ActIndRunning");
                }
            }
            get { return _ActIndRunning; }
        }

        public Decimal _LayoutRootOpacity { get; set; }
        public Decimal LayoutRootOpacity
        {
            set
            {
                if (_LayoutRootOpacity != value)
                {
                    _LayoutRootOpacity = value;
                    RaisePropertyChanged("LayoutRootOpacity");
                }
            }

            get { return _LayoutRootOpacity; }
        }

        private bool _LayoutIsEnabled = true;
        public bool LayoutIsEnabled
        {
            get { return this._LayoutIsEnabled; }
            set
            {
                this._LayoutIsEnabled = value;
                RaisePropertyChanged("LayoutIsEnabled");
            }
        }

        void ShowProgressDialog(bool ShowHide)
        {
            try
            {
                if (ShowHide)
                {
                    this.ActIndRunning = true;
                    this.LayoutRootOpacity = (decimal).2;
                    LayoutIsEnabled = false;
                }
                else
                {
                    this.ActIndRunning = false;
                    this.LayoutRootOpacity = (decimal)1;
                    LayoutIsEnabled = true;
                }
            }
            catch (Exception ex) { CoreMethods.DisplayAlert("SampleApp", "Exception", "Ok"); }
        }      
    }

    public class Helpdesk
    {
        public string Name { get; set; }
        public string Description  { get; set; }
        public Object Code { get; set; }
    }

Thanks in advance.

Sign In or Register to comment.