Fragment Fragment - 1 year ago 225
Android Question

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:


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(() =>

  2. In Your first loaded activity in OnCreate
    add: UserDialogs.Init(() => this);

mww mww
Answer Source

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


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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download