使用工厂内置的MVVMCross创建视图模型? [英] Create View Model using MVVMCross built in factory?
问题描述
我遇到一种情况,我想解析"视图模型而不必手动调用IoC容器来解析视图模型依赖项.
I have a situation where I want to "resolve" view models without having to manually call out to the IoC container to resolve a viewmodels dependencies.
我假设Mvvmcross内部具有一个视图模型工厂,用于在注入视图模型的依赖时创建视图模型.
I'm assuming that internally Mvvmcross has a viewmodel factory used to create viewmodels while injecting their dependencies.
我可以直接打电话吗?如果是这样,它叫什么名字,这些工厂位于什么命名空间中?
Can I call into that directly? If so, what is it called and what namespaces do these factories reside in?
基本上,我想做的是为tabcontroller创建一个"HomeViewModel",但不想对每个"tabs"视图模型的依赖项解析进行硬编码.
Essentially what I'm trying to do is create a "HomeViewModel" for a tabcontroller but don't want to hardcode the dependency resolution for each "tabs" view model.
推荐答案
我个人要做的是在我的HomeViewModel
中使用IoC来获取嵌套的ViewModel
的所有依赖项,并在实例化时将它们传递给它们.他们.像这样的东西:
What I personally do is to use IoC in my HomeViewModel
to get the all the dependencies of the nested ViewModel
s and pass them along when I instantiate them. So something in the likes of:
private ITwitterService _twitterService;
private IFacebookService _facebookService;
private IDataProcessingService _dataProcessingService;
private TwitterViewModel _twitterViewModel;
private FacebookViewModel _facebookViewModel;
public HomeViewModel(ITwitterService t, IFacebookService f, IDataProcessingService d)
{
_twitterService = t;
_facebookService = f;
_dataProcessingService = d;
_twitterViewModel = new TwitterViewModel(_twitterService, _dataProcessingService);
_facebookViewModel = new FacebookViewModel(_facebookService, _dataProcessingService);
}
然后,我只需在选项卡中使用ViewModel
的那些实例即可.
Then I simply use those instances of my ViewModel
s in my tabs.
这篇关于使用工厂内置的MVVMCross创建视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!