Building IG Outlook Part 4 - Creating Custom Region Adapters

This is the fourth video in a series that will take you step-by-step on building a Prism application that mimics Microsoft Outlook.  In this video we create custom region adapters for the Infragistics XamRibbon and XamOutlookBar controls that are defined as regions in our application’s shell.

The code you really care about are the two region adapters.

XamRibbon

#if !SILVERLIGHT
using Infragistics.Windows.Ribbon;
#else
using Infragistics.Controls.Menus;
#endif

namespace IgOutlook.Infrastructure.Prism
{
    public class XamRibbonRegionAdapter : RegionAdapterBase<XamRibbon>
    {
        public XamRibbonRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, XamRibbon regionTarget)
        {
            if (region == null) throw new ArgumentNullException("region");
            if (regionTarget == null) throw new ArgumentNullException("regionTarget");

            region.ActiveViews.CollectionChanged += (s, args) =>
            {
                switch (args.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        {
                            foreach (Object view in args.NewItems)
                            {
                                AddViewToRegion(view, regionTarget);
                            }
                            break;
                        }
                    case NotifyCollectionChangedAction.Remove:
                        {
                            foreach (Object view in args.OldItems)
                            {
                                RemoveViewFromRegion(view, regionTarget);
                            }
                            break;
                        }
                    default:
                        {
                            // Do nothing.
                            break;
                        }
                }
            };
        }

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

        static void AddViewToRegion(Object view, XamRibbon xamRibbon)
        {
            //TODO: implement in later video
        }

        static void RemoveViewFromRegion(Object view, XamRibbon xamRibbon)
        {
            //TODO: implement in later video
        }
    }
}

XamOutlookBar

#if SILVERLIGHT
using Infragistics.Controls.Menus;
#else
using Infragistics.Windows.OutlookBar;
#endif

namespace IgOutlook.Infrastructure.Prism
{
    public class XamOutlookBarRegionAdapter : RegionAdapterBase<XamOutlookBar>
    {
        public XamOutlookBarRegionAdapter(IRegionBehaviorFactory factory)
            : base (factory)
        {

        }

        protected override void Adapt(IRegion region, XamOutlookBar regionTarget)
        {
            region.ActiveViews.CollectionChanged += ((x, y) =>
                {
                    switch (y.Action)
                    {
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                            {
                                foreach (OutlookBarGroup group in y.NewItems)
                                {
                                    regionTarget.Groups.Add(group);

#if !SILVERLIGHT
                                //The WPF XamOutlookBar does not automatically select the first group in it's collection.
                                //So we must manually select the group if it is the first one in the collection, but we don't
                                //want to excute this code every time a new group is added, only if the first group is the current group being added.
                                if (regionTarget.Groups[0] == group)
                                {
                                    regionTarget.SelectedGroup = group;
                                }
#endif

                                }
                                break;
                            }
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                            {
                                foreach (OutlookBarGroup group in y.NewItems)
                                {
                                    regionTarget.Groups.Remove(group);
                                }
                                break;
                            }
                    }
                });
        }

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

Of course don’t forget to register your mappings in your bootstrapper.

protected override Microsoft.Practices.Prism.Regions.RegionAdapterMappings ConfigureRegionAdapterMappings()
{
    RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
    mappings.RegisterMapping(typeof(XamOutlookBar), Container.Resolve<XamOutlookBarRegionAdapter>());
    mappings.RegisterMapping(typeof(XamRibbon), Container.Resolve<XamRibbonRegionAdapter>());
    return mappings;
}

 

Watch the video on Xaml TV


Add a Comment

Be the first one to add a comment. Please Login or Register to add comment.