Using UserDialogs in Android
Asked Answered
T

1

5

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);
Titi answered 24/10, 2016 at 12:17 Comment(0)
S
12

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.

Scarabaeus answered 24/10, 2016 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.