Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UserDialogs in Android

I'm stucked with usage of plugin Acr.UserDialogs in android app, based on MVVMCross. In PCL project i used IUserDialog in viewmodel constructor injection.

I have installed Acr.UserDialogs package both in PCL and in Droid project, but when i run app, it throws:

In android, you must call UserDialogs.Init(Activity) from your first activity OR UserDialogs.Init(App) from your custom application OR provide a factory function to get the current top activity via UserDialogs.Init(() => supply top activity)

I tryed to call in my viewModel:

UserDialogs.Init(this);

But Init is not recognized And calling of UserDialogs.Instance.Loading ().Hide(); in app throws the same issue.

How it should be initialized in android project?

Upd: Final solution to workaround this looks like:

  1. In PCL project App.cs add: Mvx.RegisterSingleton(() => UserDialogs.Instance);
  2. In Your first loaded activity in OnCreate add: UserDialogs.Init(() => this);
like image 479
Fragment Avatar asked Oct 24 '16 12:10

Fragment


1 Answers

This error is very clearly. You can't initialize it in viewModel, You can only do that in your main activity.

FAQ

I'm getting a nullreferenceexception when using loading.

This happens when you run loading (or almost any dialog) from the constructor of your page or viewmodel. The view hasn't been rendered yet, therefore there is nothing to render to.

Android Initialization In your MainActivity

UserDialogs.Init(this);
OR UserDialogs.Init(() => provide your own top level activity provider)
OR MvvmCross - UserDialogs.Init(() => Mvx.Resolve<IMvxTopActivity>().Activity)
OR Xamarin.Forms - UserDialogs.Init(() => (Activity)Forms.Context)

GitHub docs.

like image 135
M. Wiśnicki Avatar answered Sep 23 '22 13:09

M. Wiśnicki