【问题标题】:Prism - Unity - Can't resolve IRegionBehaviorFactory in ConfigureRegionAdapterMappingsPrism - Unity - 无法解析 ConfigureRegionAdapterMappings 中的 IRegionBehaviorFactory
【发布时间】:2018-01-21 01:04:31
【问题描述】:

我是 Prism 的新手,我有一个应用程序,我需要一个自定义区域映射。

当我尝试使用 ServiceLocator 解析 IRegionBehaviorFactory 时,我收到错误:

尝试获取 IRegionBehaviorFactory 类型的实例时发生激活错误,键“”

我知道这是因为 RegionBehaviorFactory 类接收 Microsoft.Practices.Unity.IServiceLocator,但在 Unity 7.0 中我将其用作 ServiceLocator Unity.ServiceLocation.UnityServiceLocator

我该怎么办? 这是我的 bootstrapper.cs

 class Bootstrapper : UnityBootstrapper
{
    private UnityContainer uc = new UnityContainer();

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

        UnityServiceLocator locator = new UnityServiceLocator(uc);

        ServiceLocator.SetLocatorProvider(() => locator);
    }


    protected override DependencyObject CreateShell()
    {
        return ServiceLocator.Current.GetInstance<wMain>();
    }

    protected override void InitializeShell()
    {
        Application.Current.MainWindow = (wMain)this.Shell;
        Application.Current.MainWindow.Show();
    }

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

    protected override void InitializeModules()
    {
        base.InitializeModules();
    }

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

        uc.RegisterType<IServiceLocator, UnityServiceLocator>();
        uc.RegisterType<IRegionBehaviorFactory,  RegionBehaviorFactory>();

        Application.Current.Resources.Add("IoC", uc);
    }

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        var mappings = base.ConfigureRegionAdapterMappings();

        var aa = ServiceLocator.Current.GetInstance<IRegionBehaviorFactory>();

        mappings.RegisterMapping(typeof(RadPaneGroup), new RadPaneGroupRegionAdapter(uc.Resolve<RegionBehaviorFactory>()));

        return mappings;
    }
}

谢谢

【问题讨论】:

    标签: c# wpf unity-container prism


    【解决方案1】:

    为什么要创建新的容器和服务定位器?那堂课全错了。您不需要创建它们,因为已经为您创建了包含所有服务的容器。这就是您使用 UnityBootstrapper 的原因。 ServiceLocator 也已经为您设置好了。

    您应该阅读这些示例以更好地了解如何使用 Prism:https://github.com/PrismLibrary/Prism-Samples-Wpf

    另外一点建议,不要像现在这样使用 ServiceLocator。这是不好的做法。坚持使用常见的 DI 模式并使用 ctor injectionin 或通过从容器中解析对象来解析对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多