打开选定ViewModel的视图

时间:2013-06-10 14:40:11

标签: xamarin mvvmcross

我正在努力找到正确的方法来做到这一点。我有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类,但它没有被加载。

1 个答案:

答案 0 :(得分:1)

导航是通过Android Intent和WindowsPhone Uri之类的东西完成的 - 因此必须通过可序列化类型完成 - 请参阅:

尝试将代码更改为:

  ShowViewModel<MyViewModel>(new { id = item.Id });

收到MyViewModel之类的:

  public void Init(int id)
  {
     MyItem = myItemLookupService.Lookup(id);      
  }

此外,请记住,您几乎无法控制应用的生命周期 - 它可能随时被“竖立”。所以不要只是因为MyViewModel收到导航请求,那么该请求的项目将始终在内存中 - 您可能需要再次创建或获取它。