How to replace MvxDefaultViewModelLocator in MVVMCross application

I would like to collapse my own ViewModelLocator to provide a try / catch / log wrapper to create a ViewModel in the center of the application in order to be able to do this. I have to replace the default MvxDefaultViewModelLocator. But I did not find a way to replace the general one, only to introduce a specific type of VM.

How do I add my own ViewModelLocator class as the default?

+1
windows-phone-7 xamarin.android mvvmcross
May 2 '12 at 10:15
source share
2 answers

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()); // to disable the default ViewModelLocator, use: // base.UseDefaultViewModelLocator = false; } } 



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.

0
May 2 '12 at 10:32
source share

I just re-read your question ... and I answered another, right? Sorry!

Try again...




By default, MvxApplication provides no way to override this element. If you think this would be useful, then by all means raise the problem or pull GitHub.

However, without changing the Mvx code, one way to achieve the effect that I think you are looking for is to simply replace the IMvxViewModelLocatorFinder interface in the IoC / ServiceProvider structure - you can easily implement:

  public class MyFinder : IMvxViewModelLocatorFinder { public IMvxViewModelLocator FindLocator(MvxShowViewModelRequest request) { return new MyDefaultViewModelLocator(); } } 

and could you then insert this (overwrite the application record in IoC) during the InitializeLastChance part of the installation for each client project?




Alternatively, you can go even higher if you want - instead, you can replace IMvxViewModelLoader instead

0
May 2 '12 at 10:53
source share



All Articles