如何注册导航视图 [英] How to register Views for Navigation
问题描述
我的项目结构如下:
Modules
--- ModuleA
---ViewA
---ViewModelA
---Module
--- ModuleB
---ViewB
---ViewModelB
---Module
MyApplication
---Shell.xaml
---Bootstrapper
MyApplication.Infrastructure
---
---
现在,在ModuleA.Module中:
Now, In ModuleA.Module:
[ModuleExport(typeof(Module), InitializationMode = InitializationMode.WhenAvailable)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class Module : IModule
{
IRegionManager _regionManager;
[ImportingConstructor]
public Module(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ViewA));
}
}
现在,在ModuleB.Module中:
Now, In ModuleB.Module:
[ModuleExport(typeof(Module), InitializationMode = InitializationMode.WhenAvailable)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class Module : IModule
{
IRegionManager _regionManager;
[ImportingConstructor]
public Module(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void Initialize()
{
_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ViewB));
}
}
在Shell.xaml中:
In Shell.xaml:
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="Show View A" />
<MenuItem Header="Show View B" />
</Menu>
<ContentControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.ContentRegion}"/>
</DockPanel>
在Bootstrapper.cs中
In Bootstrapper.cs
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA.Module).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB.Module).Assembly));
}
运行应用程序时,出现错误,指出未为ContentRegion找到导出或多个导出.我可以理解,我正在同一地区注册两个视图,所以会收到该错误.
When I run the application, I get an error specifying that No Exports or Multiple Exports were found for ContentRegion. I can understand that I am registering both of my views in the same region, so I get that error.
但是我不知道如何出于导航目的注册视图,因此,当我单击MenuItem Show View A
时,ViewA应该出现在内容区域中.对于ViewB也是如此.
But I don't know how to register my views for navigation purpose, so that when I click on MenuItem Show View A
, ViewA should appear in Content Region. Similarly for ViewB.
推荐答案
您是否尝试过阅读文档?
Have you tried reading the docs?
使用MEF,您可以简单地导出具有指定名称的视图类型.
Using MEF, you can simply export the view type with the specified name.
[Export("InboxView")]
public partial class InboxView : UserControl
这篇关于如何注册导航视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!