我有两个模块:ModuleLogging和ModuleItems.我的Shell声明:
<Grid>
<DockPanel LastChildFill="True">
<ContentControl DockPanel.Dock="Top" prism:RegionManager.RegionName="{x:Static inf:RegionNames.TheUpperRegion}" Margin="5" />
<ContentControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.TheBottomRegion}" Margin="5"/>
</DockPanel>
<ContentControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.TheWholeRegion}" Margin="5" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
首先ModuleLogging加载.然后,如果用户已通过身份验证,我想加载ModuleItems.正如我所理解的那样,我应该实例化
我的bootstrapper类:
protected override IModuleCatalog CreateModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
catalog.AddModule(typeof(ModuleLogging));
//I've not added ModuleItems as I want to check whether the user is authorized
return catalog;
}
Run Code Online (Sandbox Code Playgroud)
我试图打电话ModuleItems给ModuleLogging:
Uri viewNav = new Uri("ModuleItems", UriKind.Relative);
regionManager.RequestNavigate(RegionNames.TheUpperRegion, viewNav);
regionManager.RequestNavigate(RegionNames.TheBottomRegion, viewNav);
Run Code Online (Sandbox Code Playgroud)
但我LoggingView的ModuleLogging不会被取代ToolBarView的ModuleItems和DockingView的ModuleItems.我只是分别在ContentControls中看到 System.Object而 System.Object不是LoggingView透明控件.
是的,我知道记忆中没有物体,比如ModuleItems.
如何按需加载模块并导航到视图?
您可能需要对它们进行排序以使其正常工作:
我们按顺序看看
我没有看到没有任何理由不将您的模块添加到目录中,但是在认证之后您可能不想要的是初始化模块.要按需要初始化模块,可以使用标志将其添加到目录中OnDemand:
模块将在请求时初始化,而不是在应用程序启动时自动初始化.
在你的引导程序中:
protected override void ConfigureModuleCatalog()
{
Type ModuleItemsType = typeof(ModuleItems);
ModuleCatalog.AddModule(new ModuleInfo()
{
ModuleName = ModuleItemsType.Name,
ModuleType = ModuleItemsType.AssemblyQualifiedName,
InitializationMode = InitializationMode.OnDemand
});
Run Code Online (Sandbox Code Playgroud)
然后在身份验证成功后,您可以初始化您的模块:
private void OnAuthenticated(object sender, EventArgs args)
{
this.moduleManager.LoadModule("ModuleItems");
}
Run Code Online (Sandbox Code Playgroud)
其中moduleManager是一个实例Microsoft.Practices.Prism.Modularity.IModuleManager(最好注入你的类构造函数)
您还需要注册要在容器中导航到的视图(或者如果您正在使用viewmodel-first的viewmodel).执行此操作的最佳位置可能是模块ModuleItems的初始化,只需确保在其构造函数中注入容器:
public ModuleItems(IUnityContainer container)
{
_container = container;
}
public void Initialize()
{
_container.RegisterType<object, ToolBarView>(typeof(ToolBarView).FullName);
_container.RegisterType<object, DockingView>(typeof(DockingView).FullName);
// rest of initialisation
}
Run Code Online (Sandbox Code Playgroud)
请注意,为了使RequestNavigate工作,您必须将视图注册为对象,并且还必须提供其全名作为name参数.
同时使用Module作为导航目标(就像你在这里做的那样:Uri viewNav = new Uri("ModuleItems", UriKind.Relative);可能不是最好的主意.模块对象通常只是一个临时对象,默认情况下,一旦你的模块被初始化就被抛弃了.
最后,一旦你的模块加载,你可以请求导航.您可以使用上面提到的moduleManager来挂钩LoadModuleCompleted事件:
this.moduleManager.LoadModuleCompleted += (s, e)
{
if (e.ModuleInfo.ModuleName == "ModuleItems")
{
regionManager.RequestNavigate(
RegionNames.TheUpperRegion,
typeof(ToolBarView).FullName),
result =>
{
// you can check here if the navigation was successful
});
regionManager.RequestNavigate(
RegionNames.TheBottomRegion,
typeof(DockingView).FullName));
}
}
Run Code Online (Sandbox Code Playgroud)