WPF, Prism, MEF. Register region adapter within a module?

4.7k views Asked by At

Is it possible to register a region adapter within a module?

I have a ContentControl in my Shell.xaml set to region "MainRegion" that currently gets populated with a module containing the AvalonDock control. I currently have the AvalonDock region adapter in my Shell app but would like to place it in the module and register itself. I want to keep this program flexible so that if we decide to use something other than AvalonDock, I can easily use another module without having to modify my Shell assembly (removing the avalondock region adapter).

I imagine something like this is possible. Has anyone done this before?

In bootstrapper right now is:

protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
    RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
    var regionBehaviorFactory = Container.GetExportedValue<IRegionBehaviorFactory>();
    var regionManager = Container.GetExportedValue<IRegionManager>();
    mappings.RegisterMapping(typeof(Pane), new AvalonRegionAdapter(regionBehaviorFactory, regionManager));

    return mappings;
}

This is what I would like to perform in the Module instead of the Shell bootstrapper.

1

There are 1 answers

0
Chris Klepeis On BEST ANSWER

Answer is here from codeplex http://compositewpf.codeplex.com/discussions/250892

The scenario you're describing is possible. Although custom region adapters are intended to be registered in the RegionAdapterMappings in the Bootstrapper's ConfigureRegionAdapterMappings method, it is possible to register a custom region adapter from within a module.

You could, for example, obtain a reference to the RegionAdapterMappings in your Module class by using constructor injection, and call the RegisterMapping method there. This is possible since there is a class named MefRegionAdapterMappings, which exports the RegionAdapterMappings as a shared export. Note that you should be aware of the timing issues that may arise due to this. You should be careful to register the custom mapping before attempting to create a region which uses that adapter.