C# 在bootstrapper CreateShell中使用来自模块的服务
我正在使用Prism和Silverlight,我的代码基于MefBootstrapper。定义如下:C# 在bootstrapper CreateShell中使用来自模块的服务,c#,silverlight,prism,mef,C#,Silverlight,Prism,Mef,我正在使用Prism和Silverlight,我的代码基于MefBootstrapper。定义如下: public class MyBootstrapper : MefBootstrapper { protected override DependencyObject CreateShell() { return this.Container.GetExportedValue<MainPage>(); } protected over
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() );
}
}
公共类MyBootstrapper:MefBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回此.Container.GetExportedValue();
}
受保护的覆盖无效初始值设置Shell()
{
base.InitializeShell();
App.Current.RootVisual=(UIElement)this.Shell;
}
受保护的覆盖Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
{
返回Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(新Uri(“/My;component/ModuleCatalog.xaml”,UriKind.Relative));
}
受保护的覆盖无效配置AggregateCatalog()
{
base.ConfigureAggregateCatalog();
//添加此程序集
this.AggregateCatalog.Catalogs.Add(newDeploymentCatalog());
}
}
主页的导入构造函数中有一个组件
位于单独的Xap中,该Xap位于模块目录中。xaml设置为初始化模式=“whenavaailable”
,因为我需要它
我使用断点进行了检查,在
CreateShell()
方法之前调用了CreateModuleCatalog()
方法,因此您可能认为我可以使用导入的模块。但是,我注意到我的模块的Initialize()
在CreateShell()之前没有被调用,为什么没有?我能做些什么来实现这一点呢?您的模块的初始化()
在CreateShell()
之前不会被调用,因为它尚未加载。
您可以使用事件查看模块何时加载
编辑:
不要将您的服务从其他模块导入到主页构造函数中。您可以尝试以下方法:
moduleManager.LoadModuleCompleted += ModuleManagerLoadModuleCompleted;
...
private void ModuleManagerLoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
{
if(e.ModuleInfo.ModuleName == "YourModuleName")
{
var service = ServiceLocator.Current.GetInstance<IService>();
...
moduleManager.LoadModuleCompleted -= ModuleManagerLoadModuleCompleted;
}
}
moduleManager.LoadModuleCompleted+=moduleManager LoadModuleCompleted;
...
私有void ModuleManager LoadModuleCompleted(对象发送方,LoadModuleCompletedEventArgs e)
{
if(e.ModuleInfo.ModuleName==“YourModuleName”)
{
var service=ServiceLocator.Current.GetInstance();
...
moduleManager.LoadModuleCompleted-=moduleManager LoadModuleCompleted;
}
}
那么,在构建shell之前,我如何等待它呢?CreateShell方法是一个同步方法,它希望立即返回值。。。