【问题标题】:Unable to Initialise or load prism modules on demand无法按需初始化或加载 prism 模块
【发布时间】:2018-01-17 09:26:59
【问题描述】:

我希望我的模块按需加载,因为我有一个困惑,如果我们不打算最初初始化我们的模块,那么在加载之后我们可以如何以及在哪里编写代码来执行和加载模块。 作为参考,我尝试过的只是将 ShellViewModel 代码放在这里。 请让我知道我还需要做什么才能加载按需视图,或者如果您有任何好的演示参考,请告诉我

 public class ShellViewModel
    {

        public ShellViewModel(IModuleEnumerator moduleEnumerator, IModuleLoader moduleLoader, IRegionManager regionManager)
        {
            this.Initialize(moduleEnumerator, moduleLoader, regionManager);
        }

        public ICommand LoadModuleA { get; set; }

        public ICommand LoadModuleB { get; set; }

        public IModuleLoader ModuleLoader { get; set; }

        public IModuleEnumerator ModuleEnumerator { get; set; }

        public IRegionManager RegionManager { get; set; }

        private void Initialize(IModuleEnumerator moduleEnumerator, IModuleLoader moduleLoader, IRegionManager regionManager)
        {
            // Initialize command properties
            this.LoadModuleA = new LoadModuleACommand(this);
            this.LoadModuleB = new LoadModuleBCommand(this);

            // Initialize module properties
            this.ModuleEnumerator = moduleEnumerator;
            this.ModuleLoader = moduleLoader;
            this.RegionManager = regionManager;
        }

    }
}

下面是引导类

public class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            var shell = Container.Resolve<Shell>();
            var shellViewModel = Container.Resolve<ShellViewModel>();
            shell.DataContext = shellViewModel;
            shell.Show();
            return shell;
        }

        protected override IModuleEnumerator GetModuleEnumerator()
        {
            return new DirectoryLookupModuleEnumerator(@".\Modules");
        }
    }

如果需要发布更多代码,请告诉我我无法知道如何按需加载模块以及实际代码需要在哪里编写(即在哪个部分)

【问题讨论】:

    标签: c# wpf xaml mvvm prism


    【解决方案1】:

    您不会将目录模块目录用于按需模块。

    要按需加载模块,您需要指定它们应加载到模块目录中,并将 InitializationMode 设置为 OnDemand。完成此操作后,您需要在应用程序中编写请求加载模块的代码。

    使用属性将模块指定为按需使用,如以下代码示例所示。

    protected override void ConfigureModuleCatalog() { . . . Type moduleCType = typeof(ModuleC); this.ModuleCatalog.AddModule(new ModuleInfo() { ModuleName = moduleCType.Name, ModuleType = moduleCType.AssemblyQualifiedName, InitializationMode = InitializationMode.OnDemand }); . . . }

    在按需指定模块后,应用程序可以请求加载该模块。想要启动加载的代码需要获取引导程序在容器中注册的IModuleManager服务的引用。

    private void OnLoadModuleCClick(object sender, RoutedEventArgs e) { moduleManager.LoadModule("ModuleC"); }

    您可以在文档中阅读所有相关信息:http://prismlibrary.github.io/docs/wpf/Modules.html

    【讨论】:

    • 感谢@Brian Lagunas 我遵循你的复数视野课程...我请求请添加更多包含一个小演示的章节,即按需使用的好方法以及如何维护模块之间的依赖关系。 ..
    • 小问题当 [Module(ModuleName = "ModuleA")] [ModuleDependency("ModuleD")]...这个模块依赖将在调用模块的初始化方法时或什么时候被调用? ??????
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多