RegionManager无法正确注入

时间:2018-12-03 22:16:42

标签: prism

我正在尝试将使用旧的Prism 4.0的应用程序传递给最新的Prism 7.1.0.431

我快完成了,一切都编译了。依赖性注入已更新为使用最新的Unity。因此,当我看到注射工作有些正常时,一切似乎都回到了轨道。

尽管我仍然对模块加载有疑问:无法解决区域管理器。我认为我的初始化代码中缺少某些内容,但是找不到任何相关的文档。尝试进入所有Prism.Wpf示例,但可以找到相关代码。

根据我在查找问题答案时看到的代码,在模块中注入区域管理器可能不是一个好习惯,但请记住,这是一个庞大的应用程序,并且希望避免进行过多更改尽可能

这是我遇到的异常错误:

  

例外:Prism.Modularity.ModuleInitializeException:初始化模块“ AdvancedExportModule”时发生异常。       -异常消息为:依赖项解析失败,类型='Codex.Modules.AdvancedExport.AdvancedExportModule',名称='(无)'。   发生以下异常:正在解决。   异常是:InvalidOperationException-当前类型Prism.Regions.IRegionManager是一个接口,无法构造。您是否缺少类型映射?

我是否缺少用于Unity正确映射和注入RegionManager的初始化代码?

这是代码示例,我尝试了其中的大部分,希望它足以让您了解问题所在... 这是我的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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您做得太多,您的替代中有错误的事情。示例: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,以了解应该如何调用覆盖。本质上,他们不应该互相打电话,而应该自己做。

相关问题