UserDialogs Loading does not show up

2019-03-04 09:39发布

问题:

I am trying to see Loading progress as follows, but it does not show up.

View.cs

ViewModel.SelectedCommand.Execute(null);

ViewModel.cs

public ICommand SelectedCommand
{
    get
    {
       return new MvxAsyncCommand(async () =>
       {
         // the following does not show loading
          using (UserDialogs.Instance.Loading("Loading..."))
          {
             var task = await _classroomService.GetClassRoomAsync(SelectedClassroom.Id);
             ObservableCollection<ClassroomViewModel> class = new ObservableCollection<ClassroomViewModel>(task.ConvertAll(x => new ClassViewModel(x)));
          }
       });
      }
 }

Another example

public ICommand ReloadCommand
{
    get
    {
      return new MvxAsyncCommand(async () =>
      {
          await RefreshList();
       });
     }
}

// the following also does not show loading
private async Task RefreshList()
{
   using (UserDialogs.Instance.Loading("Loading..."))
   {
       var task = await _classService.GetClasses();
   }
}

回答1:

If you are using Acr.MvvmCross.Plugins.UserDialogs see that it's depreated and you should use directly Acr.UserDialogs.

Check if you have correctly initialized it as follows:

You have to register it in App.cs of your PCL project:

Mvx.RegisterSingleton<IUserDialogs>(() => UserDialogs.Instance);

And init from the android platform project in your main activity:

UserDialogs.Init(() => Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity)

Another thing to take into account is that you should inject it in your constructor as an IUserDialogs (you can use the static Instance way but it adds more flexibility and it is more testable by injecting it):

private readonly IUserDialogs _dialogs;
public ProgressViewModel(IUserDialogs dialogs)
{
    this._dialogs = dialogs;
}

and use it like

private async Task RefreshList()
{
   using (this._dialogs.Loading("Loading..."))
   {
       try
       {
           var task = await this._classService.GetClasses();
       }
       catch(Exception exc)
       {
           // This is done only for debugging to check if here lies the problem
           throw exc;
       }
   }
}

You can check if it is properly working by calling something like

public ICommand MyTestCommand
{
    get
    {
       return new MvxAsyncCommand(async () =>
       {
         // the following should should Loading for 3 seconds
          using (this._dialogs.Loading("Loading..."))
          {
             await Task.Delay(TimeSpan.FromSeconds(3));
          }
       });
    }
 }

HIH