我正在使用 prism 4.1 和 mef 来开发前台应用程序,但我遇到了问题。有时在模块加载过程中,应用程序会因异常而失败
无法加载文件或程序集“Sl.Common.Model,版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = null”或其依赖项之一。找不到指定的文件。
模块具有依赖关系(AuditModule 和 LoadersModule 依赖于 CommonModule)。
<Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
<!-- language: lang-xml -->
<Modularity:ModuleInfo Ref="Sl.Common.Model.xap" InitializationMode="WhenAvailable" ModuleName="CommonModule"/>
<Modularity:ModuleInfo Ref="Sl.Loaders.xap" ModuleName="LoadersModule">
<Modularity:ModuleInfo.DependsOn>
<sys:String>CommonModule</sys:String>
</Modularity:ModuleInfo.DependsOn>
</Modularity:ModuleInfo>
<Modularity:ModuleInfo Ref="Sl.Audit.xap" ModuleName="AuditModule">
<Modularity:ModuleInfo.DependsOn>
<sys:String>CommonModule</sys:String>
</Modularity:ModuleInfo.DependsOn>
</Modularity:ModuleInfo>
</Modularity:ModuleCatalog>
应用程序有时可以正常启动,但是当 AuditModule 或 LoadersModule 由于“CommonModule”而无法解决时,我遇到了一个问题,上面描述了异常。
public class Bootstrapper : MefBootstrapper
{
private const string ModuleCatalogUri = "/ModerationSlUserInteface;component/ModulesCatalog.xaml";
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StackPanelRegionAdapter).Assembly));
}
protected override IModuleCatalog CreateModuleCatalog()
{
var catalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri(ModuleCatalogUri, UriKind.Relative));
return catalog;
}
protected override DependencyObject CreateShell()
{
return this.Container.GetExportedValue<Shell>();
}
protected override void InitializeShell()
{
//base.InitializeShell();
Application.Current.RootVisual = (UIElement)this.Shell;
}
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
var stackPanelAdapterInstance = ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>();
mappings.RegisterMapping(typeof(StackPanel), stackPanelAdapterInstance);
return mappings;
}
}
代码可以从这里获取
PS 在 Shell.xaml.cs 中,当我在 if (e.ModuleInfo.ModuleName == LoadersModuleName) 上打断点时
如果第一个模块是 CommonModule 则应用程序启动正常。否则它会例外。
[Export]
public partial class Shell : UserControl, IPartImportsSatisfiedNotification
{
private const string LoadersModuleName = "LoadersModule";
private static Uri LoadersViewUri = new Uri("/LoadersView", UriKind.Relative);
public Shell()
{
this.InitializeComponent();
}
[Import(AllowRecomposition = false)]
public IModuleManager ModuleManager;
[Import(AllowRecomposition = false)]
public IRegionManager RegionManager;
public void OnImportsSatisfied()
{
this.ModuleManager.LoadModuleCompleted += (s, e) =>
{
if (e.ModuleInfo.ModuleName == LoadersModuleName)
{
this.RegionManager.RequestNavigate(RegionNames.MainRegion, LoadersViewUri);
}
};
}
}