问题描述:

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.

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?

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.

网友答案:

What I personally do is to use IoC in my HomeViewModel to get the all the dependencies of the nested ViewModels 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);
}

Then I simply use those instances of my ViewModels in my tabs.

相关阅读:
Top