1

我正在努力寻找正确的方法来做到这一点。我有一个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 

知道这里发生了什么吗?我有一个MyView从 MvxViewController 继承的类,但它没有被加载。

4

1 回答 1

1

导航是通过 AndroidIntent和 WindowsPhone之类的东西完成Uri的 - 所以它必须通过可序列化的类型来完成 - 请参阅:

尝试将您的代码更改为:

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

接收MyViewModel如下:

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

另外,请记住,您对应用程序的生命周期几乎没有控制权——它可能随时被“墓碑化”。所以不要期望仅仅因为MyViewModel接收到导航请求,那么该请求的项目将始终在内存中 - 您可能需要重新创建或获取它。

于 2013-06-10T14:53:58.030 回答