简体   繁体   中英

load Prism modules from dll files (at startup)

Atm in my application I do like this:

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:

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() :

There is currently no moduleTypeLoader in the ModuleManager that can retrieve the specified module.

EDIT: this is my 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:

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.

One easier method not to enter path manually is, obtain it from 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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