2012-03-02 80 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

相关问题