我使用棱镜4.1与mef开发前台应用程序,我遇到了问题。有时,在模块加载过程的应用程序失败,异常Silverlight Prism无法解析加载模块,但并不总是
无法加载文件或程序集“Sl.Common.Model,版本= 1.0.0.0,文化=中立, 公钥=空”或它的一个依赖。找不到指定的文件。
模块具有依赖关系(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”上面descibed例外来解决。
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;
}
}
的代码可以从here
P.S.采取在Shell.xaml.cs中,当我打到 if if(e.ModuleInfo.ModuleName == LoadersModuleName)
如果第一个Module是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);
}
};
}
}
谢谢。我已经使它在启动应用程序中引用CommonModule。但之前它怎么能工作? – Grigory