ViewModdelLocator.Register method usage

Nov 27, 2013 at 9:12 AM
We are using Prism RT for one of our Windows 8 Apps and have found it really helpful in getting things setup and started. Good job you guys :)

One things I was wondering about the other day, though, is there is a method called Register in the ViewModelLocator class that apparently registers ViewModels so they don't have to be created again and again and GetViewModelForView method is able to serve up the ViewModel. However, the choice of Registering has been left up to the user, in that the Rgister method is not called from AutoWireViewModelChanged method where ViewModel is first instantiated.

Now, I'm just a bit confused, where exactly to call this register method in my App, seeing as the AutoWireViewModel property if set in XAML causes the whole ViewModel construction process to become a blackbox for me.

Just wondering if it'd be a good idea to expose some kind of property in the ViewModelLocator itself that could be set from App.Xaml.Cs. Something like EnableViewModelRegistration. Setting this property to True will cause all the ViewModel factories to be automatically registered.

Can some wise person point me in the right direction here? :)
Nov 27, 2013 at 7:46 PM
Hello xearo,

The ViewModelLocator.Register method allows you to provide a delegate that tells the ViewModelLocator how to construct your view model instances, providing required constructor dependencies.

The AutoWireModelChanged method calls GetViewModelForView which checks the factories dictionary for a matching view. The Register method simply allows you to populate the factories dictionary.

Typically I would put the calls to ViewModelLocator.Register in the App.OnInitialize method (assuming App derives from MvvmAppBase).

Hope this helps.