如何使用视图切换导航在Prism 6 WPF应用程序中注册模块?
我开始在MSVS 2015 Professional(russified)中开发Prism 6 WPF视图切换应用程序。 下面是我的解决方案表示(我在我的MSVS中请原谅俄语):
下面是我的应用程序的Bootstrepper:
class Bootstrapper : UnityBootstrapper { protected override DependencyObject CreateShell() { return Container.Resolve(); } protected override void InitializeShell() { Application.Current.MainWindow.Show(); } }
我的两个模块都必须加载应用程序,并且每个模块都有Model in Models文件夹,View in Views文件夹和ViewModel文件夹中的ViewModel。 (根据技术指配,我的应用程序必须具有多达20-22个这样的模块。)应用程序用户将通过MainWindow的MainNavigationRegion中的radiobuttons切换这些视图。 下面是MainWindow XAML(没有radiobuttons):
我是Prism的初学者,我的问题是:如何在Bootstrepper类中注册我的两个模块(AuthorizationModule和CalibrationModule)? 我应该创建自己的实现IModuleCatalog的类吗? 例如AggregateModuleCatalog:IModuleCatalog {…}并在Bootstrepper中输入以下代码:
protected override IModuleCatalog CreateModuleCatalog() { return new AggregateModuleCatalog(); }
或者使用Prism.Modularity.ConfigurationModuleCatalog并在Bootstrepper中输入以下代码:
protected override IModuleCatalog CreateModuleCatalog() { return new ConfigurationModuleCatalog(); }
现在:我应该在Bootstrepper中定义ConfigureModuleCatalog方法来显示每个模块的定义,下载和初始化方式吗? 像这样:
protected override void ConfigureModuleCatalog() { // Module "Authorisation" is defined in the code. Type moduleAuthType = typeof(AuthorizationModule); ModuleCatalog.AddModule(new ModuleInfo(moduleAuthType.Name, moduleAuthType.AssemblyQualifiedName)); // Module "Calibration" is defined in the code: Type moduleCalibrType = typeof(CalibrationModule); ModuleCatalog.AddModule(new ModuleInfo(moduleCalibrType.Name, moduleCalibrType.AssemblyQualifiedName)); }
最后:我应该配置UnityContainer以及如何配置? 例如:
protected override void ConfigureContainer() { base.ConfigureContainer(); . . . . . // What code must I write here? . . . . . }
我已经阅读了以下材料:1)视图切换导航快速入门使用Prism Library 5.0 for WPF。 2)模块化快速入门使用Prism Library 5.0 for WPF。 3)使用Prism Library 5.0 for WPF进行导航。 4)使用Prism Library 5.0进行WPF的模块化应用程序开发。 但由于我第一次认识Prism,我初学者不能指向正确的方向并寻求你的帮助。 所以我的问题是: 我如何在我的应用程序中注册我的模块?
据我所知,你必须在Unity中将你的视图注册为命名对象。
就像是
Container.RegisterType
或者对于Prism 6.0
Container.RegisterTypeForNavigation("FirstView"); Container.RegisterTypeForNavigation("SecondView");
或者你可以直接注册它(但上面的扩展方法更方便和可读)
Container.RegisterType(typeof(object), typeof(FirstView), "FirstView");
在您的IModule.Initialize()
方法中(如果视图在您的模块中)或在您的引导程序中(如果视图在您的应用程序中)。
您使用的名称可能与类型名称不同,即Container.RegisterType
。 请记住,命名字符串是导航中使用的字符串。
**更新:对于Prism 6.0,它是Container.RegisterTypeForNavigation
,看源 **
因此,要导航到此View,您将使用regionManager.RequestNavigate("MainRegion", new Uri("First", UriKind.Relative));
。
这适用于棱镜5.它应该仍然适用于棱镜6。
另外,您可以将IUnityContainer
注入模块,以便注册自己的对象。
namespace Module.MyModule { [Module(ModuleName="MyModule", OnDemand=false)] public class MyModule : IModule { private readonly IUnityContainer container; public MyModule(IUnityContainer container) { if (container == null) throw new ArgumentNullException("container"); this.container = container; } public void Initialize() { // Prism 6.0 uses RegisterTypeForNavigation from the Prism.Unity namespace this.container.RegisterTypeForNavigation("SecondView"); } } }