Prism MVVM:通过配置文件按需加载模块

时间:2017-02-28 14:09:02

标签: mvvm module load prism ondemand

我想要一个带有配置文件的Prism MVVM应用程序,我可以根据需要定义在预定义区域中加载哪些模块。

因此,如果我单击一个按钮,它应检查配置文件并加载模块并将其放入特定区域。

PRISM Modularity Quickstart这将是ModuleE对吗?

有人可以做一个小例子吗?

编辑: 到目前为止我有这个:

MainWindow.xaml

<Window x:Class="MmuTest2.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ItemsControl Grid.Row="0"
                      Grid.Column="0"
                      prism:RegionManager.RegionName="ModuleA" />
        <ItemsControl Grid.Row="0"
                      Grid.Column="1"
                      prism:RegionManager.RegionName="ModuleB" />
        <ItemsControl Grid.Row="1"
                      Grid.Column="0"
                      prism:RegionManager.RegionName="ModuleC" />
        <Button
            Grid.Row="1"
            Grid.Column="1"
            Command="{Binding LoadCommand}">Load Modules on demand</Button>
    </Grid>

</Window>

MainWindowViewModel.cs

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;

namespace MmuTest2.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Unity Application";

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public MainWindowViewModel()
        {

            this.LoadCommand = new DelegateCommand(
                () => {

                    // Load Modules!

                },
                () => true
            );
                    }

        public ICommand LoadCommand { get; private set; }
    }
}

ModuleA.cs

using Prism.Modularity;
using Prism.Regions;
using System;

namespace ModuleA
{
    [Module(ModuleName = "ModuleA")]
    public class ModuleA : IModule
    {
        IRegionManager _regionManager;

        public ModuleA(RegionManager regionManager)
        {
            _regionManager = regionManager;
        }

        public void Initialize()
        {
            this
            ._regionManager
            .RegisterViewWithRegion("ModuleA", typeof(Views.ModuleAView));
        }
    }
}

Bootstrapper.cs

using System;
using Microsoft.Practices.Unity;
using Prism.Unity;
using MmuTest2.Views;
using System.Windows;
using Prism.Modularity;

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

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog catalog = new ConfigurationModuleCatalog();
            return catalog;

        }

        protected override void ConfigureModuleCatalog()
        {
            ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
            moduleCatalog.AddModule(typeof(ModuleA.ModuleA));
        }
    }
}

EDIT2: 我找到了quick and dirty way来通过目录监控来动态加载模块。 现在我只需要一种方法来指定新模块应该通过配置文件显示在哪个区域。

1 个答案:

答案 0 :(得分:0)

您根本不必阅读配置文件。您只需在准备就绪时使用IModuleManager加载模块。实现IModule的类将负责将视图注入区域。

参见以下样本:

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/7-Modules%20-%20AppConfig

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/7-Modules%20-%20LoadManual