How to register modules in Prism 6 WPF application with navigation for switching views?

I'm starting to develop a Prism 6 WPF application for switching windows in MSVS 2015 Professional (Russified). Below is my solution (I apologize for the bag in Russian in my MSVS):

enter image description here

The following is an example Bootstrepper of my application:

class Bootstrapper : UnityBootstrapper { protected override DependencyObject CreateShell() { return Container.Resolve<MainWindow>(); } protected override void InitializeShell() { Application.Current.MainWindow.Show(); } } 

Both of my modules must be loaded with the application, and each of them has a folder "Model in models", "View in the folder" Views "and" View model "in the folder" ViewModels ". (In accordance with the technical specifications, my application should have up to 20-22 of such modules.) The application user will view these views using radio objects in MainWindow MainNavigationRegion. Below is MainWindow XAML (without radio blocks):

 <Window x:Class="FlowmeterConfigurator.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Title="{Binding Title}" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Border Grid.Column="0" Grid.Row="2" Background="LightGray" MinWidth="250" Margin="5,0,0,5"> <ItemsControl x:Name="NavigationItemsControl" prism:RegionManager.RegionName="MainNavigationRegion" Grid.Column="0" Margin="5" Padding="5" /> </Border> <ContentControl prism:RegionManager.RegionName="MainContentRegion" Grid.Column="1" Grid.Row="2" Margin="5,0,5,5" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/> </Grid> </Window> 

I start in Prism, and my problem is: how do I register my two modules (AuthorizationModule and CalibrationModule) in the Bootstrepper class? Should I create my own class that implements IModuleCatalog? For example, AggregateModuleCatalog: IModuleCatalog {...} and enter the following code into Bootstrepper:

 protected override IModuleCatalog CreateModuleCatalog() { return new AggregateModuleCatalog(); } 

Or use Prism.Modularity.ConfigurationModuleCatalog and put the following code in Bootstrepper:

 protected override IModuleCatalog CreateModuleCatalog() { return new ConfigurationModuleCatalog(); } 

Now: Do I have to define the ConfigureModuleCatalog method in Bootstrepper to show how each module is defined, loaded, and initialized? Like this:

 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)); } 

And the last: should I configure UnityContainer and how? For instance:

 protected override void ConfigureContainer() { base.ConfigureContainer(); . . . . . // What code must I write here? . . . . . } 

I read the following materials: 1) Switching navigation using the QuickStart browser using Prism Library 5.0 for WPF. 2) QuickStarts modularity using Prism Library 5.0 for WPF. 3) Navigation using Prism Library 5.0 for WPF. 4) Modular application development using Prism Library 5.0 for WPF. But because of my first acquaintance with Prisms, I, as a beginner, can not navigate in the right direction and ask for help. Therefore, my question is: How do I register in my application?

+1
source share
1 answer

As far as I remember, you should register your views as named objects in Unity.

Sort of

 Container.RegisterType<Object, FirstView>("FirstView"); Container.RegisterType<Object, SecondView>("SecondView"); 

or for Prism 6.0

 Container.RegisterTypeForNavigation<FirstView>("FirstView"); Container.RegisterTypeForNavigation<SecondView>("SecondView"); 

alternatively, you can directly register it (but the above extension methods are more convenient and readable)

 Container.RegisterType(typeof(object), typeof(FirstView), "FirstView"); 

in your IModule.Initialize() method (if the view is inside your module) or in your boot device (if the view is in your application).

The name used may differ from the type name, i.e. Container.RegisterType<Object, FirstView>("First"); . Just remember that a named string is used in navigation.

** Update: for Prism 6.0 this is Container.RegisterTypeForNavigation<FirstView>("First"); , see source **

So, to get to this view, you should use regionManager.RequestNavigate("MainRegion", new Uri("First", UriKind.Relative)); .

This worked on Prism 5. However, it should still apply to Prism 6.

As an additional note, you can introduce IUnityContainer in your modules so that they can register their own objects.

 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>("SecondView"); } } } 
+1
source

Source: https://habr.com/ru/post/903481/


All Articles