Unity & Prism Modularity - Load problems

牧云@^-^@ 提交于 2019-12-25 05:05:13

问题


I need your help again please. I am working on a application with a modular concept.

I want to use Prism and Unity for it. I have looked at the quickstart example from Prism and I also read this article on MSDN.

My actual code looks like:

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        return this.Container.Resolve<Shell>();
    }

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

        var window = this.Shell as Window;
        if (window != null)
        {
            Application.Current.MainWindow = window;
        }
        else
        {
            throw new ArgumentException("The shell has to be a window.");
        }
    }


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

My config:

<configuration>
  <configSections>
    <section name="modules" 
             type="Microsoft.Practices.Prism.Modularity.ModulesConfigurationSection, Microsoft.Practices.Prism"/>
  </configSections>
  <modules>
    <module assemblyFile="Modules/MyApp.Module1.dll"
            moduleType="MyApp.Module1.Module1Module, MyApp.Module1"
            moduleName="Module1" />
  </modules>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

I have now two problems, the first is that the catalog is not loaded right. For me it seems that the load method is not being called, or something like that.

The second problem is a consequence of the first I think, that the initialize method in my module is not called.

Can anybody help me?


回答1:


I've taken your code as is in a Desktop Prism project, but it works as expected.

Somewhere in your application, you will need (at minimum):

var bootstrapper = new Bootstrapper();
bootstrapper.Run();

I believe for the config section, the default is for modules to load on startup, so as soon as the Bootstrapper runs, your module should be loaded and initialized. This is exactly what happens for me locally.

This is what my module "initialization" looks like:

using System;
using System.Windows;
using Microsoft.Practices.Prism.Modularity;

namespace MyApp.Module1
{
    class Module1Module : IModule
    {
        public void Initialize()
        {
            MessageBox.Show("Hello world!");
        }
    }
}

One last thing to check would be to see if it can find the MyApp.Module1.dll in the directory it's looking in, but you would get a ModuleTypeLoadingException after a FileNotFoundException first-chance exception if this wasn't the case.

Edit: here is the complete source code for the test solution I made. I cannot think of any differences that make it work for me, but not for you. Check it out.



来源:https://stackoverflow.com/questions/15774171/unity-prism-modularity-load-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!