为MvxTabViewController托管和初始化多个MVVMCross ViewModel [英] Hosting and initialising multiple MVVMCross ViewModels for the MvxTabViewController
问题描述
我需要在iOS中创建一个Tab视图.我试图通过简单地将UITabBar添加到MvxViewController来做到这一点,但是却无法正常工作(请参阅
I need to create a Tab view in iOS. I tried to do this by simply adding a UITabBar to an MvxViewController but I could not get that to work (See this question if interested)
我现在正在尝试使用MVVMCross方法,而Apple似乎将我推向了TabController.
I am now trying to go use the approach MVVMCross and Apple seem to push me towards, a TabController.
在我们的核心项目中,我们有4个ViewModel,它们使用ShowViewModel调用从一个或多个位置显示,并使用customerNumber进行初始化.
In our Core project we have 4 ViewModels that are displayed from one or more places using a ShowViewModel call and are initialised with a customerNumber.
- CustomerViewModel
- CustomerOrdersViewModel
- CustomerHistoryViewModel
- ReturnsViewModel
在Android和Windows应用商店中,我们为每个这些ViewModel提供了单独的视图.我不想更改我们的Core实现以支持iOS.我将创建一个名为TabbedCustomerViewModel
的新ViewModel,它将具有4个属性,每个托管的ViewModels具有一个属性.
In Android and Windows Store we have separate Views for each of these ViewModels. I do not want to change our Core implementation to support iOS. I will create a new ViewModel called TabbedCustomerViewModel
and that will have 4 properties, one for each of the hosted ViewModels.
使用在iOS项目中注册的自定义MvxTouchViewPresenter,我可以侦听显示客户"视图之一的请求,然后将请求"切换为TabbedCustomerViewModel
.我已经完成了工作,创建了新的ViewModel,并将传递给原始VM的初始化参数传递给了拦截VM的Init方法.
Using a custom MvxTouchViewPresenter registered in the iOS project I can listen for requests to Show one of the Customer views and then switch the Request to TabbedCustomerViewModel
. I have that working, the new ViewModel gets created and the initialisation paramaters that would have been passed to the original VM are passed to the intercepting VM's Init method.
我遇到的问题是知道如何初始化托管的VM.我假设我将不得不ctor,init和手动启动它们.有没有人对如何执行此操作有任何想法?
The problem I am having is knowing how I should be initialising the hosted VMs. I assume I am going to have to ctor, init and Start them manually. Has anyone got any thoughts on how to do this?
N- 25 Tab教程不必为此担心,因为其托管的VM不是独立的,因此没有Init和Start依赖项
The N-25 Tab Tutorial does not have to worry about this as its hosted VMs are not standalone so don't have Init and Start dependencies
推荐答案
我也已经初始化了正在运行的VM.当视图加载时,它会通过查看ParentViewController来检查它是否位于选项卡式UI中.
I have the initialisation of the VMs working now too. When the View loads it checks if it is within a Tabbed UI by looking at the ParentViewController.
如果是,它将在新的TabbedCustomerViewModel
上调用自定义方法.我已经复制了MVVMCross使用的代码,并将其添加到新的方法中
If it is it calls a custom method on the new TabbedCustomerViewModel
. I have copied the code that MVVMCross use and added it to the new Method
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);
}
TabbedCustomerViewModel
在初始化时存储了InitializationParameters和reloadedState,以便可以将其向下传递到其托管的ViewModels
The initialisationParameters and reloadedState are stored by TabbedCustomerViewModel
when it is initialised so that it can pass it down to the ViewModels it is hosting
这篇关于为MvxTabViewController托管和初始化多个MVVMCross ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!