简体   繁体   English

从dll文件加载Prism模块(启动时)

[英]load Prism modules from dll files (at startup)

Atm in my application I do like this: 在我的应用程序中,Atm会这样:

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

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

            App.Current.MainWindow = (Window)Shell;
            App.Current.MainWindow.Show();
        }     

        protected override void ConfigureModuleCatalog()
        {            
            base.ConfigureModuleCatalog();
            var moduleCatalog = (ModuleCatalog)ModuleCatalog;

            moduleCatalog.AddModule(typeof(FooModule));
            moduleCatalog.AddModule(typeof(BarModule));
        }        
    }

I would like to load FooModule and BarModule by indicating the path to the dll file , something like this: 我想通过指示dll文件的路径加载FooModule和BarModule ,如下所示:

protected override void ConfigureModuleCatalog()
{
...
            var assembly = Assembly.LoadFrom(@"libs\FooLib.dll");
            var type = assembly.GetType("FooLib.FooModule");
            moduleCatalog.AddModule(type);
...
}

but it doesn't work, I get this error message on Bootstrapper.Run() : 但它不起作用,我在Bootstrapper.Run()上收到此错误消息:

There is currently no moduleTypeLoader in the ModuleManager that can retrieve the specified module. 当前在ModuleManager中没有moduleTypeLoader可以检索指定的模块。

EDIT: this is my FooModule: 编辑:这是我的FooModule:

public class FooModule : IModule
    {
        private readonly IRegionViewRegistry _regionViewRegistry;

        public FooModule(IRegionViewRegistry registry)
        {
            _regionViewRegistry = registry;
        }

        public void Initialize()
        {
            _regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(Main));
        }
    }

Ok, try to make your ConfigureModuleCatalog looking like this: 好的,尝试使您的ConfigureModuleCatalog看起来像这样:

protected override void ConfigureModuleCatalog()
{
    string path = @"libs\FooLib.dll";
    var assembly = Assembly.LoadFrom(path);
    var type = assembly.GetType("FooLib.FooModule");
    ModuleCatalog.AddModule(new ModuleInfo
                                {
                                    ModuleName = type.Name,
                                    ModuleType = type.AssemblyQualifiedName,
                                    Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri
                                });


}

The key thing is: 关键是:

Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri    

prism checks whether Ref property refers to physical file( file:// ) and loads assembly from this file. prism检查Ref属性是否Ref物理文件( file:// )并从该文件加载程序集。

One easier method not to enter path manually is, obtain it from type->assembly->location 一种不手动输入路径的简便方法是,从type-> assembly-> location获取它

    Type module1Type = typeof(Module1.Module1);
    string path = module1Type.Assembly.Location;
    moduleCatalog.AddModule(
      new ModuleInfo()
      {
          ModuleName = module1Type.Name,
          ModuleType = module1Type.AssemblyQualifiedName,
          Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri
      });

    return moduleCatalog;

I think Prism v4 Loading modules on demand with DirectoryModuleCatalog could help. 我认为Prism v4使用DirectoryModuleCatalog按需加载模块可能会有所帮助。

UPDATE: 更新:
Sorry, just realized that reference mentioned above won't help. 抱歉,刚刚意识到上述参考文献无济于事。
Try this one from msdn - " Loading Modules on Demand " section, I think that's what you need. 尝试从msdn-按需加载模块 ”部分尝试这一操作,我认为这就是您所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM