从IntentFilter启动Activity时ViewModel null [英] ViewModel null when Activity started from IntentFilter
问题描述
我正在尝试将NFC与MvvmCross一起使用.因此,我创建了一个如下所示的IntentFilter
:
I am trying to use NFC along with MvvmCross. So I have created an IntentFilter
which looks like this:
[IntentFilter(new[]{"android.nfc.action.NDEF_DISCOVERED"},
Categories = new[] {"android.intent.category.DEFAULT"},
DataScheme = "bksv-resource", DataHost = "nmt")]
在OnResume
中,我正在寻找NfcAdapter.ActionNdefDiscovered
类型的Intent
动作.当发现NDEF,创建Activity并在其中包含数据的情况下设置Intent时,会发生什么.所有这些在Activity
中都可以正常工作,但是当想要使用MvxActivity
或MvxFragmentActivity
时会出现问题.
And in OnResume
I am looking for Intent
Actions of the type NfcAdapter.ActionNdefDiscovered
. What happens is when NDEF is discovered, the Activity gets created and an Intent is set with the data inside. All this works fine in an Activity
, however problems occur when wanting to use an MvxActivity
or MvxFragmentActivity
.
问题是,当重新创建MvxActivity
时,base.OnCreate()
调用不会返回ViewModel
并且没有给出原因,仅显示如下输出:
Problem is, when the MvxActivity
gets recreated the base.OnCreate()
call gives no ViewModel
back and no reason is given, only output like this is shown:
mvx:Diagnostic: 6.09 Loading new ViewModel from Intent with Extras
mvx:Warning: 6.13 ViewModel not loaded for view ScanView
我不确定如何找到ViewModel
的ViewModel
,但是,在这种情况下似乎找不到.为什么会这样呢?我是否需要自定义应用启动或其他内容?
I am not entirely sure how the ViewModel
is found for a View
, however, in this case it seems not to be found. Why could that be? Do I need a custom app start or something?
推荐答案
MvvmCross从传入的Intent
MvvmCross picks up its ViewModel navigation information from the passed in Intent
您可以:
- 查看MvvmCross如何在以下
Intent
中创建它: MvxAndroidViewsContainer.cs#L87
- see how MvvmCross creates it's
Intent
s in: MvxAndroidViewsContainer.cs#L120 - see how MvvmCross gets the information back out in: MvxAndroidViewsContainer.cs#L87
如果您使用自定义的非MvvmCross Intent
,则MvvmCross显然将无法获取其导航信息-因此,您必须找出一种自己创建ViewModel的方法.
If you are using a custom non-MvvmCross Intent
then MvvmCross obviously won't be able to pick up its navigation information - so you'll have to work out a way to create the ViewModel yourself.
这篇关于从IntentFilter启动Activity时ViewModel null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!