aamankhaan
aamankhaan

Reputation: 491

Mvvmcross Loading Indicator not showing

I am being using mvvmcross to develop a cross platform app, it's helps us in most prominent way.

I am facing an minor issue on Loading indicator display.

I have a property named IsLoading in my BaseViewModel

    public abstract class BaseViewModel : MvxViewModel
    {
       #region IsLoading

        private bool _IsLoading;
        public bool IsLoading
        {
            get { return _IsLoading; }
            set
            {
                _IsLoading = value;
                RaisePropertyChanged(() => IsLoading);
            }
        }

        #endregion
    }

Now, I have MainViewModel which create instance of two viewmodel inside it like below.

public class MainViewModel : BaseViewModel
{
    public DashBoardViewModel DashboardVM
    {
        get { return _DashBoardVM; }
        set
        { 
            _DashBoardVM = value;
            RaisePropertyChanged(() => DashboardVM);
        }
    }

    public AccountViewModel AccountVM
    {
        get { return _AccountVM; }
        set
        {
            _AccountVM = value;
            RaisePropertyChanged(() => AccountVM);
        }
    }
    public MainViewModel()
    {
        //To Do 
    }

    public async Task Init()
    {
        DashboardVM = new DashBoardViewModel();            
        AccountVM = new AccountViewModel();
        await Task.Delay(0);
    }
}

Below is my DashBoardViewModel

public class DashBoardViewModel : BaseViewModel
{
     public DashBoardViewModel()
     {
         IsLoading = true;
         //Code to get data
     }
}

As you can see in the above code i have setted the IsLoading = true, but at client end we don't get propertychanged event for this property.

Below is my one of client code to show progressdialog.

On Android we are using fragments, below is my MainView activity

public class MainView : MvxFragmentActivity
{
    private ViewPager _viewPager;
    private TabPageIndicator _pageIndicator;
    private MvxViewPagerFragmentAdapter _adapter;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.MainView);

        var fragments = new List<MvxViewPagerFragmentAdapter.FragmentInfo>
          {
            new MvxViewPagerFragmentAdapter.FragmentInfo
            {
              FragmentType = typeof(DashboardFragment),
              Title = "",
              ViewModel = ViewModel.DashboardVM
            },

            new MvxViewPagerFragmentAdapter.FragmentInfo
            {
              FragmentType = typeof(MyProfileFragment),
              Title = "",
              ViewModel = ViewModel.AccountVM
            }
          };
        _viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
        _adapter = new MvxViewPagerFragmentAdapter(this, SupportFragmentManager, fragments);
        _viewPager.Adapter = _adapter;

        _pageIndicator = FindViewById<TabPageIndicator>(Resource.Id.viewPagerIndicator);

        _pageIndicator.SetViewPager(_viewPager);
        _pageIndicator.CurrentItem = 0;
    }              
}

Below is my DashBoardFragment class

public class DashboardFragment : MvxFragment
{
     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, 
                                       Bundle savedInstanceState)
    {
        var ignore = base.OnCreateView(inflater, container, savedInstanceState);
        var viewToReturn = this.BindingInflate(Resource.Layout.Dashboard, null);

                    #region MVVMCROSS Binding

        var set = this.CreateBindingSet<DashboardFragment, DashBoardViewModel>();

        #endregion

        #region Handle Progress bar

        //initializing bindableprogress 
        var _bindableProgress = new BindableProgress(Activity);
        //Binding with viewmodel property 
        set.Bind(_bindableProgress).For(m => m.IsLoading).To(vm => vm.IsLoading);

        #endregion

        #region Bind the set

        set.Apply();

        #endregion

        return viewToReturn;
    }
}

Note : BindableProgress is the class which has the property on which loading is been displayed and it is working fine on simple ViewModel.

Please help me to fix this.

Thanks Aaman

Upvotes: 1

Views: 1252

Answers (1)

PkL728
PkL728

Reputation: 955

I don't believe this is correct:

public class DashBoardViewModel : BaseViewModel
{
 public DashBoardViewModel()
 {
     IsLoading = true;
     //Code to get data
 }
}

You probably don't want this happening in the constructor as you are creating the view models, not showing them yet. That is why you don't see an indicator because you set IsLoading to true but that is not the active screen.

Does something like this work?

public async Task Init()
{
    IsLoading = true;
    DashboardVM = new DashBoardViewModel();            
    AccountVM = new AccountViewModel();
    await Task.Delay(0);
    IsLoading = false;
}

The idea here being you should be showing the loading indicator for the MainView model during initialization of the child view models.

Upvotes: 1

Related Questions