RegionManager 注入不正确

RegionManager not injecting properly

我正在尝试将我们使用旧版 Prism 4.0 的应用程序转移到最新的 Prism 7.1.0.431

我快完成了,一切都可以编译。依赖注入已更新为使用最新的 Unity。所以一切似乎都回到了正轨,因为我看到注入有点工作。

虽然我仍然遇到模块加载问题:区域管理器无法解决。我想我在我的初始化代码中遗漏了一些东西,但找不到任何相关的文档。尝试进入所有 Prism.Wpf 个示例,但可以找到相关代码。

从我在搜索问题答案时看到的代码来看,在模块中注入区域管理器可能不是一个好的做法,但请耐心等待,现在它是一个巨大的应用程序,希望避免更改它尽可能:

这是我遇到的异常错误:

EXCEPTION: Prism.Modularity.ModuleInitializeException: An exception occurred while initializing module 'AdvancedExportModule'. - The exception message was: Resolution of the dependency failed, type = 'Codex.Modules.AdvancedExport.AdvancedExportModule', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, Prism.Regions.IRegionManager, is an interface and cannot be constructed. Are you missing a type mapping?

我是否遗漏了 RegionManager 要由 Unity 正确映射和注入的一些初始化代码?

这是代码示例,我尽量简化其中的大部分内容,希望它足以让您了解问题所在... 这是我的 App.xaml:

<prism:PrismApplication x:Class="Codex.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/">
 <Application.Resources>
  <ResourceDictionary Source="Resources/Merged.xaml"/>
 </Application.Resources>
</prism:PrismApplication>

在我后面的代码中 App.xaml.cs

    namespace MyNamespace
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Threading;

    using Prism.Ioc;
    using Prism.Logging;
    using Prism.Modularity;
    using Prism.Unity;

    public partial class App : PrismApplication
    {
        private static ILoggerFacade Logger { get; set; }

        public static void Main()
        {
            var application = new App();
            application.InitializeComponent();
            application.Run();
        }

        protected override void OnStartup(StartupEventArgs startupEventArgs)
        {           
            base.OnStartup(startupEventArgs);
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            var modulesFilePaths = new Dictionary<string, string>();
            modulesFilePaths.Add("Namespace.Modules.Module1.dll", "Namespace.Modules.AdvancedExport.Module1Module");

            var pathToExecutingLibrary = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;

            foreach (KeyValuePair<string, string> moduleFilePath in modulesFilePaths)
            {
                var referenceUri = Path.Combine(pathToExecutingLibrary, moduleFilePath.Key);
                var assembly = Assembly.LoadFrom(referenceUri);
                var type = assembly.GetType(moduleFilePath.Value);

                moduleCatalog.AddModule(
                    new ModuleInfo(type)
                    {
                        ModuleName = type.Name,
                        Ref = referenceUri,
                        InitializationMode = InitializationMode.WhenAvailable
                    });
            }

            moduleCatalog.Initialize();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            ConfigureViewModelLocator();
            var containerExtension = CreateContainerExtension();
            containerRegistry.RegisterInstance(containerExtension);

            // These methods have been commented out they are use to register all the types of the application.
            //RegisterSettings(containerRegistry);
            //RegisterServices(containerRegistry);
            //RegisterHandlers(containerRegistry);
            //RegisterWrappers(containerRegistry);

            containerRegistry.RegisterInstance(Dispatcher.CurrentDispatcher);
        }

        protected override Window CreateShell()
        {
            Window mainShell = Container.Resolve<MainShell>();
            return mainShell;
        }
    }
}

你在覆盖中做的太多了,而且是错误的。示例:RegisterTypes 应该只注册类型...

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    // this has already been called by the base class: ConfigureViewModelLocator();
    // this has also been called by the base class: var containerExtension = CreateContainerExtension();
    // containerRegistry.RegisterInstance(containerExtension);

    // These methods have been commented out they are use to register all the types of the application.
    //RegisterSettings(containerRegistry);
    //RegisterServices(containerRegistry);
    //RegisterHandlers(containerRegistry);
    //RegisterWrappers(containerRegistry);

    containerRegistry.RegisterInstance(Dispatcher.CurrentDispatcher);
}

您应该查看 source code 以了解应该如何调用覆盖。本质上,他们不应该互相调用,只是做自己的工作。