Search code examples
wpfprismunity-containermefmodularity

PRISM module registration


I have a WPF application with a simple tabular region layout as follows:

-----------------------------
|   Region 1  |   Region 2  |
-----------------------------
|   Region 3  |   Region 4  |
-----------------------------

I want to offer the ability to register modules and their position based on module/app configuration e.g. Register "Module1" in Region 1 and set its rowspan to 2 so that it overlaps region3 or perhaps register Module1, Module2, Module3 and Module4 in there respective regions, or alternatively register Module1 so that it consumes the entire grid.

My current thinking is to define the four regions in the Shell, load the modules from a XAML module catalog and set the row/column/rowspan/columnspan from the app.config however it would be ideal if there was some way in which I could extend the module catalog so that each module entry could directly specify the grid settings directly (much like attached dependency properties), I could then read these during module initalisation and register the modules in each region as required.

Hope this makes sense if not let me know and I'll try to clarify - I may well be overcomplicating things!


Solution

  • First of all, in the question you are confusing views with modules. A module can be initialized without adding any views at all to your UI, or it may add several. So, assuming that in your case a module will expose exactly one type of view, it would be better to describe the setup as "add View1 in Region1" etc.

    To solve your problem, you can use the Grid-based approach in your example like this:

    1. Declare your Grid in XAML with as many cells/regions as you want, just like your example. (Since your app will have a greatly dynamic layout, this could be better done manually in code. But if you 're willing to accept some hardwired row/column limits, XAML will work fine too and it will be simpler)
    2. Define a Prism event which your add-on modules will use to notify a "master" module that they have been initialized. The master module will load before any add-on module and take care of the layout. When raising this event, the add-on modules will include the types of views they have configured the container with as part of the event arguments.
    3. When initializing, the master module will subscribe to the "module initialized" event. Whenever the event is raised (an add-on module has been initialized), the master module will resolve a view of the appropriate type from the container (remember: the add-on module has told us what type of view to resolve).

    It is then very simple to achieve your goal like this:

    var view = container.Resolve(typeOfViewFromAddOnModule);
    var uiElement = (UIElement)view; // because that's what it's going to be
    Grid.SetColumn(uiElement, X);
    Grid.SetRow(uiElement, Y);
    Grid.SetColumnSpan(uiElement, Z);
    Grid.SetRowSpan(uiElement, W);
    var region = // get a reference to your Grid region here
    region.Add(view);
    

    You are free to determine the values of X, Y, Z, W in whatever way you wish (load them from configuration, auto-assign them if they are not present, have the add-on module specify them in the "module initialized" event, etc etc).