3

我有一个 WPF 应用程序,它使用Prism.WpfPrism.UnityNuGet 包(两者6.3.0)。我目前正在引导程序类(见下文)中手动在 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());
    }
}

但是,当我尝试按约定Microsoft.Practices.Unity.DuplicateTypeMappingException注册类型时,在 Unity 容器中注册类型时会得到一个。

按约定注册的代码:

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 & Unity 时如何按约定注册类型?

4

1 回答 1

4

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

UnityBootstrapper只会注册以前未注册的类型,所以你应该没问题。

于 2017-03-27T17:07:36.963 回答