我有两个模块: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>
首先加载ModuleLogging
。然后,如果用户通过身份验证,我想加载ModuleItems
。
据我所知,我应该实例化
我的引导程序类:
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;
}
我尝试从ModuleItems
致电ModuleLogging
:
Uri viewNav = new Uri("ModuleItems", UriKind.Relative);
regionManager.RequestNavigate(RegionNames.TheUpperRegion, viewNav);
regionManager.RequestNavigate(RegionNames.TheBottomRegion, viewNav);
但LoggingView
ModuleLogging
ToolBarView
并未被ModuleItems
的{{1}}和DockingView
ModuleItems
所取代。我只是在ContentControls而不是控件中看到System.Object
和System.Object
,因为LoggingView
是透明的。
是的,我知道记忆中没有对象,例如ModuleItems
。
如何按需加载模块并导航到视图?
答案 0 :(得分:6)
您可能需要对它们进行排序才能使其正常工作:
让我们按顺序看
我没有看到任何没有将您的模块添加到目录中的原因,但是在认证之后您可能不想要的是初始化模块。要按需要初始化模块,可以使用标记OnDemand
:
模块将在请求时初始化,而不是自动初始化 在申请启动时。
在你的引导程序中:
protected override void ConfigureModuleCatalog()
{
Type ModuleItemsType = typeof(ModuleItems);
ModuleCatalog.AddModule(new ModuleInfo()
{
ModuleName = ModuleItemsType.Name,
ModuleType = ModuleItemsType.AssemblyQualifiedName,
InitializationMode = InitializationMode.OnDemand
});
然后,在身份验证成功后,您可以初始化您的模块:
private void OnAuthenticated(object sender, EventArgs args)
{
this.moduleManager.LoadModule("ModuleItems");
}
其中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
}
请注意,为了使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));
}
}
答案 1 :(得分:3)