2012-09-27 36 views
1

我使用棱鏡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); 
      } 
     }; 
    } 
} 

回答

1

我們發現您也必須在模塊中添加任何引用到入口點項目中。

所以,如果你在AuditModule參考Microsoft.Practices.Prism.UnityExtensions(例如),那麼你需要一個參考MainProject添加入MainProject以及即使它沒有直接引用任何代碼。

+0

謝謝。我已經使它在啓動應用程序中引用CommonModule。但之前它怎麼能工作? – Grigory