0
votes

I want a Prism MVVM App with a config file, in which I could define which Modules are loaded in predefined regions on demand.

So if I click a button, it should check the config file and and load a module and place it into the certain region.

In the PRISM Modularity Quickstart this would be ModuleE right?

Could somebody make a small example?

EDIT: I have this so far:

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: I found a quick and dirty way to load dynamically modules via directory monitoring. Now I just need a way to specify in which region the new module should be shown via a config file.

1
You have posted a lot of code, but did nothing in the code to try and solve your issue. If you know how to read information from a config file, then you should be able to do this. - R. Richards

1 Answers

0
votes

You don't have to read the config file at all. You just load the module using the IModuleManager when you are ready. The class that implements IModule will be responsible for injecting views into regions.

See these samples:

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