wpf prism:ViewModelLocator.AutoWireViewModel="True" 不适用于未引用的程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33043978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
prism:ViewModelLocator.AutoWireViewModel="True" will not work for not referenced assemblies
提问by Briefkasten
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.
当在 shell 项目中引用模块的项目时,它工作正常。以下附加属性将注入正确的 ViewModel。
<UserControl prism:ViewModelLocator.AutoWireViewModel="True" [..]</>
<UserControl prism:ViewModelLocator.AutoWireViewModel="True" [..]</>
Unless I remove the project reference the ViewModels will not be set anymore by the prism:ViewModelLocator.AutoWireViewModel="True".
除非我删除项目引用,否则棱镜将不再设置 ViewModel: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?
有谁知道是什么原因造成的?您可以在https://github.com/mfe-/Get.the.solution.Prism.Demo项目上观察这种行为。我怎样才能解决这个问题?
回答by Brian Lagunas
Basically, when the ViewModelLocationProvider calls the _defaultViewTypeToViewModelTypeResolver, the call to Type.GetType(string) returns null.
基本上,当 ViewModelLocationProvider 调用 时_defaultViewTypeToViewModelTypeResolver,对 Type.GetType(string) 的调用返回 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:
这可能与 MEF 通常如何加载程序集有关。这似乎是 MEF 的一个常见问题,谷歌搜索会返回很多具有类似问题的结果。这是一个有同样问题的人:
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.
我个人从不使用 MEF 作为 DI 容器,因为它不是一个。但那是另一天的谈话。
EDIT:Actually, I just thought of a better way to get around this. Simply override ConfigureViewModelLocator in your bootstrapper like this:
编辑:实际上,我只是想了一个更好的方法来解决这个问题。只需像这样在引导程序中覆盖 ConfigureViewModelLocator:
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.
这样我们就可以直接向程序集询问类型,而不是试图让框架为我们解决。

