棱镜:ViewModelLocator.AutoWireViewModel = QUOT;真"不会不引用的程序工作 [英] prism:ViewModelLocator.AutoWireViewModel="True" will not work for not referenced assemblies
问题描述
我加载我模块的引导程序:
保护覆盖IModuleCatalog CreateModuleCatalog()
{
VAR moduleCatalog =新DirectoryModuleCatalog(); \Modules
moduleCatalog.ModulePath = @;
返回moduleCatalog;
}
在模块的项目在壳牌项目中引用工作正常。正确的ViewModels将通过以下附加属性被注入。
<用户控件棱镜:ViewModelLocator.AutoWireViewModel =真[..] LT; />
除非删除了项目引用的ViewModels将不会通过棱镜再设置:ViewModelLocator.AutoWireViewModel =真
有谁知道是什么原因造成的?你可以看该项目的这一行为 https://github.com/mfe- /Get.the.solution.Prism.Demo 。我该如何解决这个问题?
基本上,当ViewModelLocationProvider调用 _defaultViewTypeToViewModelTypeResolver
,调用Type.GetType(字符串)返回null。
这可能与负荷一般如何MEF组件。这似乎是与MEF和谷歌搜索将返回了很多类似的问题结果的共同probem。
这是有人用同样的问题:
的 Type.GetType使用MEF 时返回null
您可以尝试在应用程序中的探测路径添加插件位置
我个人从来没有使用MEF作为一个DI容器,因为它是一个也没有。 。但是,这是另一天的谈话</ p>
编辑:
其实,我只是想到了一个更好的办法来解决这个问题。只需在您的引导程序像这样覆盖ConfigureViewModelLocator:
保护覆盖无效ConfigureViewModelLocator()
{
群。 ConfigureViewModelLocator();
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(viewType =>
{
变种的viewName = viewType.FullName;。视图
的viewName = viewName.Replace(。的ViewModels);
VAR viewAssemblyName = viewType.GetTypeInfo()Assembly.FullName;
变种后缀= viewName.EndsWith(查看)?的模式。:视图模型;
VAR viewModelName =的String.Format(CultureInfo.InvariantCulture,{0} {1}的viewName,后缀);
VAR总成= viewType.GetTypeInfo()大会;
型变种= assembly.GetType(viewModelName,真);
返回类型;
});
}
这样我们就可以直接问组装的类型,而不是试图让该框架算起来我们。
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?
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.
这篇关于棱镜:ViewModelLocator.AutoWireViewModel = QUOT;真"不会不引用的程序工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!