本文介绍了从加载dll文件棱镜模块(在启动时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ATM在我的应用程序,我不喜欢这样的:

 类引导程序:UnityBootstrapper
    {
        保护覆盖的DependencyObject CreateShell()
        {
            返回Container.Resolve<壳>();
        }

        保护覆盖无效InitializeShell()
        {
            base.InitializeShell();

            App.Current.MainWindow =(窗口)外壳;
            App.Current.MainWindow.Show();
        }

        保护覆盖无效ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
            VAR moduleCatalog =(ModuleCatalog)ModuleCatalog;

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

我想为负荷FooModule和BarModule通过指示DLL文件的路径,是这样的:

 保护覆盖无效ConfigureModuleCatalog()
{
...
            VAR组装= Assembly.LoadFrom(@库\ FooLib.dll);
            VAR类型= assembly.GetType(FooLib.FooModule);
            moduleCatalog.AddModule(类型);
...
}
 

,但它不工作,我得到Bootstrapper.Run()此错误消息:

目前在ModuleManager会没有moduleTypeLoader可以检索指定的模块。

编辑:这是我的FooModule:

 公共类FooModule:IModule的
    {
        私人只读IRegionViewRegistry _regionViewRegistry;

        公共FooModule(IRegionViewRegistry注册表)
        {
            _regionViewRegistry =登记;
        }

        公共无效初始化()
        {
            _regionViewRegistry.RegisterViewWithRegion(MainRegion的typeof(主));
        }
    }
 

解决方案

好了,尽量让你的 ConfigureModuleCatalog 寻找这样的:

 保护覆盖无效ConfigureModuleCatalog()
{
    字符串路径= @库\ FooLib.dll;
    VAR组装= Assembly.LoadFrom(路径);
    VAR类型= assembly.GetType(FooLib.FooModule);
    ModuleCatalog.AddModule(新ModuleInfo
                                {
                                    模块名= type.Name,
                                    ModuleType = type.AssemblyQualifiedName,
                                    参考=新的URI(路径,UriKind.RelativeOrAbsolute).AbsoluteUri
                                });


}
 

最关键的一点是:

 参考值=新的URI(路径,UriKind.RelativeOrAbsolute).AbsoluteUri
 

棱镜检查参考属性是否是指物理文件(文件:// )并加载组件从该文件。

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.

这篇关于从加载dll文件棱镜模块(在启动时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:38