我正在努力找到正确的方法来做到这一点。我有MyCollectionViewModel
,其中包含MyViewModel
的可观察集合。我将此绑定到视图MyCollectionView
,并选择绑定回MyCollectionViewModel
中名为OpenMyViewCommand
的命令的项目。到目前为止一切顺利:
private MvxCommand<MyViewModel> _openMyViewCommand;
public ICommand OpenMyViewCommand
{
get {
_openMyViewCommand= _openMyViewCommand?? new MvxCommand<MyViewModel>(OpenMyView);
return _openSubViewCommand;
}
}
现在当我触摸列表视图中的一个项目时,它将触发命令并调用方法OpenMyView
将选定的MyViewModel
传递给方法。现在的问题是,无论我尝试什么,我似乎都可以ShowViewModel
显示所选模型的视图。我已经尝试了各种各样的东西,但直觉上,我会期待像这样的非常简单的工作:
private void OpenMyView(MyViewModel item) {
ShowViewModel<MyViewModel > (item);
}
但是,当然,事实并非如此。我得到的错误看起来像这样:
013-06-10 10:46:05.849 MyAppIPhoneUI[51198:c07] mvx: Warning: 5.83 Problem creating viewModel of type MyViewModel - problem MvxException: Failed to resolve parameter for parameter model of type MyModel when creating MyApp.Core.MyViewModel
at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00042] in /MvvmCross-3/CrossCore/Cirrious.CrossCore/IoC/MvxSimpleIoCContainer.cs:377
at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00030] in /MvvmCross-3/CrossCore/Cirrious.CrossCore/IoC/MvxSimpleIoCContainer.cs:266
at Cirrious.CrossCore.Mvx.IocConstruct (System.Type t) [0x00006] in /MvvmCross-3/CrossCore/Cirrious.CrossCore/Mvx.cs:115
at Cirrious.MvvmCross.ViewModels.MvxDefaultViewModelLocator.TryLoad (System.Type viewModelType, IMvxBundle parameterValues, IMvxBundle savedState, IMvxViewModel& viewModel) [0x00004] in /MvvmCross-3/Cirrious/Cirrious.MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs:27
013-06-10 10:24:11.330 MyAppIPhoneUI[50878:c07] mvx: Warning: 6.87 Exception masked MvxException: Failed to load ViewModel for type MyApp.Core.MyViewModel from locator MvxDefaultViewModelLocator
at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator) [0x00023] in /MvxViewModelLoader.cs:46
知道这里发生了什么吗?我有一个继承自MvxViewController的MyView
类,但它没有被加载。
答案 0 :(得分:1)
导航是通过Android Intent
和WindowsPhone Uri
之类的东西完成的 - 因此必须通过可序列化类型完成 - 请参阅:
尝试将代码更改为:
ShowViewModel<MyViewModel>(new { id = item.Id });
收到MyViewModel
之类的:
public void Init(int id)
{
MyItem = myItemLookupService.Lookup(id);
}
此外,请记住,您几乎无法控制应用的生命周期 - 它可能随时被“竖立”。所以不要只是因为MyViewModel
收到导航请求,那么该请求的项目将始终在内存中 - 您可能需要再次创建或获取它。