UserDialogs Loading does not show up
Asked Answered
F

2

0

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();
   }
}
Foldaway answered 7/3, 2018 at 22:39 Comment(3)
where does it not show up? Android? iOS? Have you had a look at the output? Can you post it?Segment
I am working on Android environment. I cannot able yo see loading dialogFoldaway
Try using UserDialogs.Instance.ShowLoading("Loading...") to show it and UserDialogs.Instance.HideLoading(); to hide it afterwardsBlues
D
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

Deflect answered 9/3, 2018 at 13:12 Comment(9)
Still does not show. I believe it does not like to run in the background thread. I do not know how to handle.Foldaway
You can try wrapping it in this.InvokeOnMainThread(() => {...});. Does your console shows any output of what might be happening?Deflect
could you please insert into your answer?Foldaway
I've thought it twice and it shouldn't be wrapped because you won't be able to await your classService correctly. Furthermore the examples given in github.com/aritchie/userdialogs/blob/master/src/Samples/Samples/… are all async commands so I don't think that is the problem. Could you please see in your output if you have something that points out to the problem? Also wrap your GetClasses() call in a try catch so you can see if that is breaking and thus finishes the dialog before you can see itDeflect
why did you add await Task.Delay(TimeSpan.FromSeconds(3));, in the given link, they use it for demo purpose.Foldaway
I also added it as demo/test, as you can see it is inside MyTestCommand. The RefreshList stays as it was wrapped in a try catch to see if you get an exceptionDeflect
Still did not work. I have added try & catch as well.Foldaway
but does it never enter in the catch? And have you checked your output to see if there is some logs in there?Deflect
It never enters to catchFoldaway
M
0

I dont like this approuch but it works

Device.BeginInvokeOnMainThread(async () =>
{

    try
    {
        using (UserDialogs.Instance.Loading(("Loading...")))
        {
            await Task.Delay(300);
            await _syncController.SyncData();           
            //Your Service code
        }
    }
    catch (Exception ex)
    {
        var val = ex.Message;
        UserDialogs.Instance.Alert("Test", val.ToString(), "Ok");
    }
});
Marriage answered 20/1, 2020 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.