REQ: Hands-On Lab for PRISM with MEF

Topics: Prism v4 - WPF 4
Aug 18, 2010 at 11:54 PM
Hi Experts, many articles and some sample apps showed me how interesting PRISM is. My monolitic MVVM Application is fascinating complex ;-) Therefore i try to understand PRISM 4. The Documentation is very good, i only miss PRISM MEF specifics. Since two days i fail with a simple 'Hello World' with MEF. A basic MEF knowledge and some study on the PRISM Examples were helpful to import and intialize the module, but i fail at the last steps (RegionManager). I am sure you work hard and the PRISM 4 Version will be nice and smart. But, please dont forget the PRISM Newbies with a Hands-On Lab for PRISM with MEF. Thank you, marc
Aug 19, 2010 at 1:54 PM

Hi All,

for anyone who try to figure out a PRISM MEF Hello World Example, this work for me after i fight a lot with the CTP Hands-On Lab :


bootstrapper.cs

class Bootstrapper : MefBootstrapper
    {
        /// <summary>
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        /// </remarks>
        protected override DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<Shell>();   
        }

        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        protected override void InitializeShell()
        {
            //  Rufe InitializeShell aus MefBootstrapper auf
            base.InitializeShell(); 
            //  Setze this.Shell als MainWindow
            App.Current.MainWindow = (Window)this.Shell; 
            //  Stelle MainWindow dar
            App.Current.MainWindow.Show();
        }

        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

        /// <summary>
        /// Creates the <see cref="IModuleCatalog"/> used by Prism.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns>
        /// </returns>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }

        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();
        }

        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();  
            // HelloWorldModule is referenced in in the project and directly in code.
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(HelloWorldModule).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Shell).Assembly));
            
        }
    }


shell.xaml.cs

[Export]
    public partial class Shell : Window
    {
        public Shell()
        {
            this.InitializeComponent();
        }
    }


HelloWorldModule.cs

 [ModuleExport(typeof(HelloWorldModule))]
    public class HelloWorldModule : IModule
    {
        private readonly IRegionManager regionManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regionManager"></param>
        /// <remarks>
        /// Modify the constructor of the HelloWorldModule class to obtain a region manager instance through constructor dependency injection
        /// and store it in the regionManager instance variable. To do this, the constructor has to take a parameter of type 
        /// Microsoft.Practices.Composite.Regions.IRegionManager.
        /// </remarks>
        [ImportingConstructor]
        public HelloWorldModule(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
        }

        /// <summary>
        /// Notifies the module that it has be initialized.
        /// </summary>
        public void Initialize()
        {
            // In the Initialize method, invoke the RegisterViewWithRegion method on the RegionManager instance. This method registers a region 
            // name with its associated view type in the region view registry; the registry is responsible for registering and retrieving of 
            // these mappings. The RegisterViewWithRegion method has two overloads. When you want to register a view directly, you use the first
            // overload that requires two parameters, the region name and the type of the view. This is shown in the following code.

            regionManager.RegisterViewWithRegion("MainRegion", typeof(HelloWorldView));

            // The UI composition approach used in the preceding code is known as View Discovery. When using this approach, you specify the views
            // and the region where the views will be loaded. When a region is created, it asks for its associated views and automatically loads them.

        }
    }


HelloWorldView.xaml.cs

[Export]
    public partial class HelloWorldView : UserControl
    {
        public HelloWorldView()
        {
            InitializeComponent();
        }
    }

 

Aug 20, 2010 at 9:19 AM

Hi marckraemer, your example looks very good, "I Like it" - Cheers Harry

Jun 25, 2014 at 2:19 PM
Thank you so much for this example. I was missing the [Export] attribute on my module view .cs :-)