2012-03-02 29 views
0

我使用Prism和Silverlight並將我的代碼基於MefBootstrapper。該定義如下:在引導程序中使用模塊中的服務CreateShell

public class MyBootstrapper : MefBootstrapper 
{ 
    protected override DependencyObject CreateShell() 
    { 
     return this.Container.GetExportedValue<MainPage>(); 
    } 

    protected override void InitializeShell() 
    { 
     base.InitializeShell(); 

     App.Current.RootVisual = (UIElement)this.Shell; 
    } 

    protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog() 
    { 
     return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("/My;component/ModulesCatalog.xaml", UriKind.Relative)); 
    } 

    protected override void ConfigureAggregateCatalog() 
    { 
     base.ConfigureAggregateCatalog(); 

     // Add this assembly 
     this.AggregateCatalog.Catalogs.Add(new DeploymentCatalog()); 
    } 
} 

的具有的MainPage在其ImportingConstructor生活在一個獨立的XAP,在ModulesCatalog.xaml設置爲InitializationMode="WhenAvailable",因爲我需要它的時候了一個組件。

我檢查了斷點,並且在CreateShell()方法之前調用了CreateModuleCatalog()方法,所以您會認爲我可以使用導入的模塊。但是,我注意到我的模塊的Initialize()之前沒有調用CreateShell()爲什麼不呢?我能做些什麼來完成這項工作?

回答

1

您的模塊的Initialize()CreateShell()之前未被調用,因爲它尚未加載。 您可以使用IModuleManager.LoadModuleCompleted事件來查看您的模塊何時加載。

編輯:

不要從其他模塊導入您服務到構造的MainPage。你可以嘗試這樣的事情:

moduleManager.LoadModuleCompleted += ModuleManagerLoadModuleCompleted; 
... 
private void ModuleManagerLoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e) 
{ 
    if(e.ModuleInfo.ModuleName == "YourModuleName") 
    { 
     var service = ServiceLocator.Current.GetInstance<IService>(); 
     ... 
     moduleManager.LoadModuleCompleted -= ModuleManagerLoadModuleCompleted; 
    } 
} 
+0

那麼在構建我的shell之前,我該如何去等待那個呢? CreateShell方法是一種同步方法,它期望立即返回值... – Alex 2012-03-03 05:46:03

相關問題