问题
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")]
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.
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
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?
回答1:
MvvmCross picks up its ViewModel navigation information from the passed in Intent
You can:
- see how MvvmCross creates it's
Intents in: MvxAndroidViewsContainer.cs#L120 - see how MvvmCross gets the information back out in: MvxAndroidViewsContainer.cs#L87
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.
来源:https://stackoverflow.com/questions/17547700/viewmodel-null-when-activity-started-from-intentfilter