为 MvxTabViewController 托管和初始化多个 MVVMCross ViewModel

Hosting and initialising multiple MVVMCross ViewModels for the MvxTabViewController

我需要在 iOS 中创建一个选项卡视图。我试图通过简单地将 UITabBar 添加到 MvxViewController 来做到这一点,但我无法让它工作(参见 this question if interested

我现在正尝试使用 MVVMCross 方法,而 Apple 似乎将我推向了 TabController。

在我们的核心项目中,我们有 4 个 ViewModel,它们使用 ShowViewModel 调用从一个或多个位置显示,并使用 customerNumber 进行初始化。

  1. CustomerViewModel
  2. CustomerOrdersViewModel
  3. CustomerHistoryViewModel
  4. 返回视图模型

在 Android 和 Windows Store 中,我们为每个 ViewModel 提供了单独的视图。我不想更改我们的核心实现来支持 iOS。我将创建一个名为 TabbedCustomerViewModel 的新 ViewModel,它将具有 4 个属性,每个托管 ViewModel 一个。

使用在 iOS 项目中注册的自定义 MvxTouchViewPresenter,我可以侦听显示其中一个客户视图的请求,然后将请求切换到 TabbedCustomerViewModel。我有这个工作,创建了新的 ViewModel 并将传递给原始 VM 的初始化参数传递给拦截 VM 的 Init 方法。

我遇到的问题是我应该如何初始化托管虚拟机。我假设我将不得不手动构造、初始化和启动它们。有人对如何执行此操作有任何想法吗?

N-25 Tab Tutorial 不必担心这一点,因为它的托管 VM 不是独立的,因此没有 Init 和 Start 依赖项

我现在也可以对 VM 进行初始化。当视图加载时,它通过查看 ParentViewController 来检查它是否在选项卡 UI 内。

如果是,它会在新 TabbedCustomerViewModel 上调用自定义方法。我复制了 MVVMCross 使用的代码并将其添加到新方法

try
{
    mvxViewModel.CallBundleMethods("Init", this.initialisationParameters);
    if (reloadedState != null)
    {
        mvxViewModel.CallBundleMethods("ReloadState", reloadedState);
    }
    mvxViewModel.Start();
}
catch (Exception exception)
{
    throw exception.MvxWrap("Problem initialising viewModel of type {0}", mvxViewModel.GetType().Name);
}

initialisationParameters 和 reloadedState 在初始化时由 TabbedCustomerViewModel 存储,以便它可以将其传递给它托管的 ViewModels