通过与数据的链接打开MvvmCross应用程序 [英] Open MvvmCross app via link with data

查看:64
本文介绍了通过与数据的链接打开MvvmCross应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在本机Android应用程序中,您可以为清单文件中的活动定义intent-filter,当从网站或电子邮件访问特定链接(例如myprotocol://mysite.com/action/data)时可以打开该应用程序.

In native Android apps you can define an intent-filter for an activity in the Manifest file which can open the app when a specific link (e.g. myprotocol://mysite.com/action/data) is visited from a website or email.

<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链接到该视图.我似乎找不到任何有关如何加载ViewModel和获取意图数据(URL的data部分)的信息.

When I try to add this to the AndroidManifest file in an MvvmCross application, it seems that only the view gets loaded, without ViewModel linked to it. I cannot seem to find any information on how to load the ViewModel and get my intent data (the data part of the url).

有人做过吗?

推荐答案

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

I have an app that responds to scanning NFC tags and I have a similar situation.

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

My solution is to add the following in my OnCreate override right after I call base.OnCreate(bundle):

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.

As the comment says, the Intent is not one coming from MvvmCross. This means the bundle which tells MvvmCross which ViewModel to load is not present. So what I do is to create the ViewModel myself.

这篇关于通过与数据的链接打开MvvmCross应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆