如何使用视图切换导航在 Prism 6 WPF 应用程序中注册模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35476745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:41:00  来源:igfitidea点击:

How to register modules in Prism 6 WPF application with view-switching navigation?

c#wpfmvvmprism

提问by Dr.Doog

I begin develop Prism 6 WPF view-switching application in MSVS 2015 Professional (russified). Below is my solution representation (I bag your pardon for Russian in my MSVS):

我开始在 MSVS 2015 Professional (russified) 中开发 Prism 6 WPF 视图切换应用程序。以下是我的解决方案表示(我在我的 MSVS 中对俄语进行了原谅):

enter image description here

在此处输入图片说明

Below is Bootstrepper of my application:

下面是我的应用程序的 Bootstrepper:

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 Model in Models folder, View in Views folder and ViewModel in ViewModels folder. (In accordance with technical assignmen my application must have up to 20-22 such modules.) Application user will swithch these views by the radiobuttons in MainWindow's MainNavigationRegion. Below is MainWindow XAML (without radiobuttons):

我的两个模块都必须与应用程序一起加载,并且每个模块在 Models 文件夹中都有 Model,在 Views 文件夹中有 View,在 ViewModels 文件夹中有 ViewModel。(根据技术分配,我的应用程序必须有多达 20-22 个这样的模块。)应用程序用户将通过 MainWindow 的 MainNavigationRegion 中的单选按钮切换这些视图。下面是 MainWindow XAML(没有单选按钮):

<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 beginner in Prism and my problem is: How to register my two modules (AuthorizationModule and CalibrationModule) in Bootstrepper class? Should I create my own class implementing IModuleCatalog? For example AggregateModuleCatalog:IModuleCatalog{...} and put in Bootstrepper the following code:

我是 Prism 初学者,我的问题是:如何在 Bootstrepper 类中注册我的两个模块(AuthorizationModule 和 CalibrationModule)?我应该创建自己的类来实现 IModuleCatalog 吗?例如 AggregateModuleCatalog:IModuleCatalog{...} 并在 Bootstrepper 中放入以下代码:

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

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

或者使用 Prism.Modularity.ConfigurationModuleCatalog 并在 Bootstrepper 中放入以下代码:

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

Now: Should I define ConfigureModuleCatalog method in Bootstrepper to show how each module is defined, downloaded and initialized? Like this:

现在:我应该在 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));
}

And last: Should I configure UnityContainer and how? For example:

最后:我应该配置 UnityContainer 以及如何配置?例如:

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

I've read the following materials: 1) View-Switching Navigation QuickStart Using the Prism Library 5.0 for WPF. 2) Modularity QuickStarts Using the Prism Library 5.0 for WPF. 3) Navigation Using the Prism Library 5.0 for WPF. 4) Modular Application Development Using Prism Library 5.0 for WPF. But due to my first acquaintance with Prism, I'm as beginner can't orientate in correct direction and ask for your help. So my question is: How must I register my modules in my application?

我已阅读以下材料:1) 使用 Prism Library 5.0 for WPF 的视图切换导航快速入门。2) 使用 Prism Library 5.0 for WPF 的模块化快速入门。3) 使用 Prism Library 5.0 for WPF 导航。4) 使用 WPF 的 Prism 库 5.0 进行模块化应用程序开发。但是由于我第一次接触棱镜,我作为初学者无法定位正确的方向并寻求您的帮助。所以我的问题是:我必须如何在我的应用程序中注册我的模块?

回答by Tseng

As far as I remember, you have to register your Views as named objects in Unity.

据我所知,您必须在 Unity 中将您的视图注册为命名对象。

Something like

就像是

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

or for Prism 6.0

或棱镜 6.0

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

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

或者你可以直接注册它(但上面的扩展方法更方便和可读)

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

in either your IModule.Initialize()method (if the view is inside your Module) or in your bootstrapper (if the view is in your application).

在您的IModule.Initialize()方法中(如果视图在您的模块中)或在引导程序中(如果视图在您的应用程序中)。

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

您使用的名称可以与类型名称不同,即Container.RegisterType<Object, FirstView>("First");. 请记住,命名字符串是用于导航的字符串。

**Update: For Prism 6.0 it's Container.RegisterTypeForNavigation<FirstView>("First");, see the source**

**更新:对于 Prism 6.0 Container.RegisterTypeForNavigation<FirstView>("First");,请参阅源代码**

So to navigate to this View, you'd use regionManager.RequestNavigate("MainRegion", new Uri("First", UriKind.Relative));.

因此,要导航到此视图,您需要使用regionManager.RequestNavigate("MainRegion", new Uri("First", UriKind.Relative));.

This worked for Prism 5. It should still apply for Prism 6 though.

这适用于 Prism 5。不过它仍然适用于 Prism 6。

As an additional note, you can inject your IUnityContainerinto your modules, so they can register their own objects.

作为附加说明,您可以将您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>("SecondView");
        }
    }
}