Custom Region class

Topics: Prism v4 - Silverlight 4
Feb 26, 2011 at 12:25 PM

Hello,

I would like to override this method "void Activate(object obj)" of the Region class.

Is it possible to replace this without modifying the Prism library of course?

 

Thanks,

Adrien

Feb 28, 2011 at 5:16 PM

Hi Adrien,

You could extend Region class and override the Activate method since is a virtual method. By doing this, you can provide your own logic to activate regions in your application without modifying the Prism library.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 1, 2011 at 7:41 AM
Edited Mar 1, 2011 at 7:42 AM

Yes, it is what I tried. But my custom class was not used.

I have missed something ...

 

How do you tell prism to use mine instead of its default class?

Developer
Mar 1, 2011 at 1:48 PM

In order to achieve what you've mentioned, you need to create a custom region adapter to indicate that a region of type CustomRegion should be created when a control of certain type is marked as a region.

For example, the SelectorRegionAdapter currently maps the selector controls to the Region region, while the ContentControlRegionAdapter maps the content controls to the SingleActiveRegion region, and so forth. You might find the following classes useful to your purpose (located in the Regions folder inside the Prism.Silverlight project of the Prism Library):

  • Region
  • SingleActiveRegion
  • AllActiveRegion
  • RegionAdapterBase
  • SelectorRegionAdapter
  • TabControlRegionAdapter
  • ItemsControlRegionAdapter
  • ContentControlRegionAdapter

I hope you find this helpful.

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

 

http://compositewpf.codeplex.com/discussions/247930 m
Mar 1, 2011 at 2:32 PM

I have already created a custom region adapter but not for the same purpose. In fact, I want to pass my viewModel throughout the activate method and not the view. And of course that crashes the application since the region needs a view to work properly. I want to modify that method for every view in my application.

I don't want to create a custom region adapter because if I do that, it will use only my custom adapter and not anymore the predefined ones and/or I will need to override all the enlisted region adapter you mentioned above.

Mar 1, 2011 at 5:36 PM

Hi Adrien,

In case you need to achieve that scenario without using a custom region adapter, you should have to create your custom region object and manually attach it to its hosting control.

In order to do so, you should do something similar to what a region adapter does, but manually. For example, here's the code of the ContentControlRegionAdapter.Adapt method:

        protected override void Adapt(IRegion region, ContentControl regionTarget)
        {
            if (regionTarget == null) throw new ArgumentNullException("regionTarget");
            bool contentIsSet = regionTarget.Content != null;
#if !SILVERLIGHT
            contentIsSet = contentIsSet || (BindingOperations.GetBinding(regionTarget, ContentControl.ContentProperty) != null);
#endif
            if (contentIsSet)
            {
                throw new InvalidOperationException(Resources.ContentControlHasContentException);
            }
            region.ActiveViews.CollectionChanged += delegate
            {
                regionTarget.Content = region.ActiveViews.FirstOrDefault();
            };
            region.Views.CollectionChanged +=
                (sender, e) =>
                {
                    if (e.Action == NotifyCollectionChangedAction.Add && region.ActiveViews.Count() == 0)
                    {
                        region.Activate(e.NewItems[0]);
                    }
                };
        }

Also, you should probably have to manually call the RegionManager.SetRegionManager and set the RegionManager.RegionName attached property in your hosting control.

You should take into account that regions aren't meant to be mapped to ViewModels, but rather to views, so the approach you're following isn't recommended by Prism's guidance.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic