Do it right ... but not really in any of the samples!
The "container" for ViewModelLocators is an MvxApplication object. By default, the conditional MvxDefaultViewModelLocator , which simply tries to instantiate ViewModel using their declared constructors that have string parameters.
If you want to use your own ViewModel locator, then the easiest way is to inherit from MvxViewModelLocator and provide either public properties or public methods that return ViewModel instances:
eg:
public class MyViewModelLocator : MvxViewModelLocator { public MyFirstViewModel CreateFirst() { return new MyFirstViewModel(); } public MySecondViewModel CreateSecond(string aParameter) { var someLookup1 = ComplicatedStaticThing1.Lookup(aParameter); var viewModel = new MySecondViewModel(someLookup1); var someLookup2 = ComplicatedStaticThing2.Lookup(aParameter, someLookup1); viewModel.DoSomething(someLookup2); return viewModel; } private readonly MyThirdViewModel _third = new MyThirdViewModel(); public MyThirdViewModel Third { get { return _third; } } }
If you want to go even lower than this, then you can also implement IMvxViewModelLocator.
To add a ViewModelLocator to an application, just create it and add it to your application - for example:
public class App : MvxApplication , IMvxServiceProducer<IMvxStartNavigation> { public App() { this.RegisterServiceInstance<IMvxStartNavigation>(new StartApplicationObject()); base.AddLocator(new MyViewModelLocator());
Note: - In addition to the development time data, I now very rarely find the need to create a custom ViewModelLocator - in general, everything I want to do can be done in the ViewModel construct.
Stuart May 2 '12 at 10:32 2012-05-02 10:32
source share