我坚持使用基于 MVVMCross 的 Android 应用程序中的插件 Acr.UserDialogs。
在 PCL 项目中,我在 viewmodel 构造函数注入(inject)中使用了 IUserDialog。
我已经在 PCL 和 Droid 项目中安装了 Acr.UserDialogs 包,但是当我运行应用程序时,它会抛出:
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)
我试图在我的 viewModel 中调用:
UserDialogs.Init(this);
但初始化不被识别
并调用 UserDialogs.Instance.Loading ().Hide();在应用程序中引发同样的问题。
它应该如何在android项目中初始化?
Upd: Final solution to workaround this looks like:
- In PCL project App.cs add: Mvx.RegisterSingleton(() => UserDialogs.Instance);
- In Your first loaded activity in OnCreate add: UserDialogs.Init(() => this);
最佳答案
这个错误非常明显。你不能在 viewModel 中初始化它,你只能在你的主要 Activity 中这样做。
常问问题
I'm getting a nullreferenceexception when using loading.
当您从
页面或 View 模型的构造函数。 View 尚未渲染
然而,因此没有什么可以渲染的。
Android 初始化 在您的 主要 ActivityUserDialogs.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.
关于android - 在 Android 中使用用户对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218229/