Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / WPF

Composite Application Library in WPF Application

Rate me:
Please Sign up or sign in to vote.
4.82/5 (30 votes)
2 Mar 2009CPOL8 min read 95.8K   3.1K   110  
WPF application built on the Composite Application Library. How to start, organize solution projects, distribute resources and build complex UI using the Presentation Model pattern.
//
// MenuController.cs
// Author: Eugene Pankov
// January 31, 2009
//
// Subscribes to following CompositePresentationEvent events:
//      MainMenuChangeEvent
//      

using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Presentation.Events;
using CWA.Infrastructure;


namespace CompositeWpfApp.Controllers
{
    /// <summary>
    /// Class <see cref="MenuController"/> is used to display menu in the MainMenuRegion.
    /// </summary>
    public class MenuController : IGeneralController
    {
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;


        public MenuController(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this.regionManager = regionManager;
            this.eventAggregator = eventAggregator;
        }

        #region IGeneralController Members

        public void Run()
        {
            eventAggregator.GetEvent<MainMenuChangeEvent>().Subscribe(SetMenuView, ThreadOption.UIThread, true);
        }

        #endregion

        /// <summary>
        /// Replaces current view displayed in the MainMenuRegion.
        /// </summary>
        /// <param name="view">New menu view.</param>
        /// <exception cref="ArgumentNullException">View is null.</exception>
        private void SetMenuView(object view)
        {
            if (view == null)
                throw new ArgumentNullException("view", "Cannot resolve main menu view.");

            IRegion region = regionManager.Regions[RegionNames.MainMenuRegion];

            if (region != null)
            {
                object currentView = region.GetView(RegionNames.MainMenuRegion);

                if (currentView != null)
                    region.Remove(currentView);

                // Display a menu
                region.Add(view, RegionNames.MainMenuRegion);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Latvia Latvia
Jevgenij lives in Riga, Latvia. He started his programmer's career in 1983 developing software for radio equipment CAD systems. Created computer graphics for TV. Developed Internet credit card processing systems for banks.
Now he is System Analyst in Accenture.

Comments and Discussions