android - 在 Android 中使用用户对话框

标签 android xamarin mvvmcross

我坚持使用基于 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:

  1. In PCL project App.cs add: Mvx.RegisterSingleton(() => UserDialogs.Instance);
  2. In Your first loaded activity in OnCreate add: UserDialogs.Init(() => this);

最佳答案

这个错误非常明显。你不能在 viewModel 中初始化它,你只能在你的主要 Activity 中这样做。

常问问题

I'm getting a nullreferenceexception when using loading.



当您从
页面或 View 模型的构造函数。 View 尚未渲染
然而,因此没有什么可以渲染的。

Android 初始化 在您的 主要 Activity
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.

关于android - 在 Android 中使用用户对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40218229/

相关文章:

iphone - MvvmCross:GestureRecognized 绑定(bind)到 ViewModel 操作

android - 如何使用 MVVMCross 验证 Xamarin.Android 中的数字

android - 如何判断 Android 应用程序是否在前台运行?

Android - 访问 MainActivity 实例

c# - Xamarin.Forms 中的水平 ListView

c# - GetAllWithChildren() 性能问题

java - Xamarin Studio - 打包错误 - "jarsigner.exe"退出,代码为 1。 (MSB6006)

android - 像这样获得类似选项卡式布局的布局(在 Activity Activity 上加下划线)

android - 从本地文件或 Google Play 安装时如何将参数传递给我的应用程序?

windows - UWP 中的多选下拉列表