Search code examples
c#wpfprismmef

prism:ViewModelLocator.AutoWireViewModel="True" will not work for not referenced assemblies


I load my modules in the bootstrapper with:

    protected override IModuleCatalog CreateModuleCatalog()
    {
        var moduleCatalog = new DirectoryModuleCatalog();
        moduleCatalog.ModulePath = @".\Modules";
        return moduleCatalog;
    }

which works fine when the project of the modules are referenced in the shell project. The correct ViewModels will be injected by the following attached property.

<UserControl prism:ViewModelLocator.AutoWireViewModel="True" [..]</>

Unless I remove the project reference the ViewModels will not be set anymore by the prism:ViewModelLocator.AutoWireViewModel="True".

Does anybody know the what causes that? You can watch this behaviour on the project https://github.com/mfe-/Get.the.solution.Prism.Demo . How can I fix this?


Solution

  • Basically, when the ViewModelLocationProvider calls the _defaultViewTypeToViewModelTypeResolver, the call to Type.GetType(string) returns null.

    This might be related to how MEF loads assemblies in general. This seems to be a common probem with MEF and a Google search will return a lot of results with similar issues. Here is someone with the same problem:

    Type.GetType returns null when using MEF

    You could try adding the plugin location in the probing path of the application.

    I personally never use MEF as a DI container, because it's not one. But that is a conversation for another day.

    EDIT: Actually, I just thought of a better way to get around this. Simply override ConfigureViewModelLocator in your bootstrapper like this:

            protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator();
    
            ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(viewType =>
            {
                var viewName = viewType.FullName;
                viewName = viewName.Replace(".Views.", ".ViewModels.");
                var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
                var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";
                var viewModelName = String.Format(CultureInfo.InvariantCulture, "{0}{1}", viewName, suffix);
    
                var assembly = viewType.GetTypeInfo().Assembly;
                var type = assembly.GetType(viewModelName, true);
    
                return type;
            });
        }
    

    This way we can ask the assembly for the type directly and not try to have the framework figure it out for us.