如何使用Unity with Prism按惯例注册类型?

时间:2017-03-27 09:32:42

标签: c# wpf unity-container prism prism-6

我有一个使用Prism.WpfPrism.Unity NuGet包(均为6.3.0)的WPF应用。我目前在一个引导程序类中手动注册Unity容器中的类型(见下文),一切都运行良好。

internal class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void InitializeShell()
    {
        Application.Current.MainWindow.Show();
    }

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();

        // Register types
        Container.RegisterType<IDialogService, DialogService>(new ContainerControlledLifetimeManager());
    }
}

但是,当我尝试按惯例注册类型时,在Unity容器中注册类型时会得到Microsoft.Practices.Unity.DuplicateTypeMappingException

按惯例代码注册:

protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    // Register types by convention
    Container.RegisterTypes(
        AllClasses.FromLoadedAssemblies(),
        WithMappings.FromMatchingInterface,
        WithName.Default,
        WithLifetime.ContainerControlled);
}

异常消息:

An attempt to override an existing mapping was detected for type Prism.Regions.IRegionNavigationContentLoader with name "", currently mapped to type Prism.Unity.Regions.UnityRegionNavigationContentLoader, to type Prism.Regions.RegionNavigationContentLoader.

使用Prism&amp ;;时,如何按惯例注册类型?团结吗

1 个答案:

答案 0 :(得分:4)

只需交换Container.RegisterTypes(...);base.ConfigureContainer();

UnityBootstrapper只会注册以前没有注册的类型,所以你应该没事。