How do I resolve ViewModels with the Prism.Mvvm ( ViewModelLocationProvider?

Feb 19, 2015 at 1:20 PM
Hi all,

In our solution we upgraded to the latest available versions (1.1 and 2.0) of StoreApps, PubSubEvents and Mvvm prism libraries.

Now we are experiencing trouble to resolve ViewModels that are located in a different assembly. Before the newest library versions we used to have this:
        ViewModelLocator.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
                var viewModelTypeName = string.Format(CultureInfo.InvariantCulture, "FutureModels.ViewModels.{0}ViewModel, FutureModels, Version=, Culture=neutral, PublicKeyToken=SN_Generated_Key", viewType.Name);
                var viewModelType = Type.GetType(viewModelTypeName);
                return viewModelType;
But that is no longer supported by the Mvvm library. We have concluded that it is not enough to simply change from ViewModelLocator to ViewModelLocationProvider. That won't bind the ViewModels and we get a black screen after the splashscreen.

How do we use the newer library and what is the correct statement to resolve ViewModels?
Feb 22, 2015 at 2:01 PM
ViewModelLocationProvider is the right type to use. If you call SetDefaultViewTypeToViewModelTypeResolver on it in your OnInitializeAsync method in your application, you should see your factory method (the lambda you are passing in) being called. From there it just becomes a question of whether your method is returning a type that the ViewModelLocationProvider can actually produce a type from. It has exception handling so if it fails to resolve the type identified by your method into an instance it will silently do nothing.

You could test that by trying to call Activator.CreateInstance on that type in your method to see if it succeeds. That is what Prism uses by default to create the view model instance under the covers (if you don't also SetDefaultViewModelFactory).

This all works, just threw together an example where it is being called just fine to confirm to myself. Shoot me an email at brian.noyes on gmail if you want me to send you that sample.
Marked as answer by PeterKlein on 2/22/2015 at 12:55 PM