通过 link 使用数据打开 MvvmCross 应用程序

Open MvvmCross app via link with data

在本机 Android 应用程序中,您可以在清单文件中为 activity 定义 intent-filter,当特定 link(例如 myprotocol://mysite.com/action/data) 从网站或电子邮件访问。

<intent-filter>
    <data android:scheme="myprotocol" android:host="mysite.com"/>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

当我尝试将其添加到 MvvmCross 应用程序中的 AndroidManifest 文件时,似乎只有视图被加载,而 ViewModel link 没有被加载。我似乎无法找到有关如何加载 ViewModel 和获取我的意图数据(url 的 data 部分)的任何信息。

以前有人做过吗?

我有一个响应扫描 NFC 标签的应用程序,我也有类似的情况。

我的解决方案是在我调用 base.OnCreate(bundle) 后立即在我的 OnCreate 覆盖中添加以下内容:

if (null == ViewModel)
{
    //This should only happen when the Intent is not an Mvx one. I.e. when having scanned
    //NFC tag. We need to load the ViewModel manually.
    var loaderService = Mvx.Resolve<IMvxViewModelLoader>();
    ViewModel = (ScanViewModel) loaderService.LoadViewModel(
        new MvxViewModelRequest(typeof (ScanViewModel), null, null, null), null);
}

正如评论所说,Intent 不是来自 MvvmCross 的。这意味着告诉 MvvmCross 要加载哪个 ViewModel 的包不存在。所以我要做的是自己创建 ViewModel。