Register RegionAdapter in Module

Topics: Prism v4 - WPF 4
Mar 23, 2011 at 5:40 PM
Edited Mar 23, 2011 at 5:44 PM

Is it possible to register a region adapter within a module? Please note that I'm using MEF

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?

Developer
Mar 23, 2011 at 5:59 PM

Hi,

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.

I hope you find this helpful.

Guido Leandro Maliandi 
http://blogs.southworks.net/gmaliandi

Mar 23, 2011 at 6:33 PM

Thanks, adding RegionAdapterMappings to my constructor worked.

Sep 10, 2012 at 12:19 PM

Hi

I am trying add a stackpanel region , where I have to add usercontrol to it from my module.(WPF app).

Can I use RegionAdapterMappings? can you please post the code?

Developer
Sep 10, 2012 at 2:17 PM
Edited Sep 10, 2012 at 2:22 PM

Hi,

I believe you could check the following thread, where the user tzuhsun posted an possible approach on how to implement a StackPanel region adapter:

Based on this approach, your region adapter could be like this:

public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
    {

        public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, StackPanel regionTarget)
        {
            if (region == null) return;

            region.Views.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case NotifyCollectionChangedAction.Add:

                        foreach (UIElement element in e.NewItems)
                        {
                            regionTarget.Children.Add(element);
                        }
                        break;

                    case NotifyCollectionChangedAction.Remove:

                        foreach (UIElement elementLoopVariable in e.OldItems)
                        {
                            var element = elementLoopVariable;

                            if (regionTarget.Children.Contains(element))
                            {
                                regionTarget.Children.Remove(element);
                            }
                        }
                        break;
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }

Once you created your region adapter, you could customize the Region Adapter Mappings for example by overriding the ConfigureRegionAdapterMappings method in your bootstrapper like this:

 

protected override Microsoft.Practices.Prism.Regions.RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
            regionAdapterMappings.RegisterMapping(typeof(StackPanel), ServiceLocator.Current.GetInstance<StackPanelRegionAdapter>());
            return regionAdapterMappings;
        }

Additionally, for more information on how to create a custom Region Adapter you could check the following section of the Prism documentation:

I hope you find this handy,

Agustin Adami
http://blogs.southworks.net/aadami