App.xaml.cs source code

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.ComponentModel.Composition;
using System.Reflection;
using MEFContrib.Library.Base;
using MEFContrib.Library.Interface.Interfaces;
using MEFContrib.Library.Interface.Arguments;
using Microsoft.Practices.Unity;

namespace BasicWPFApp
{

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application, IMefApplication
    {
        /// <summary>
        /// Instantiate a MEF BootStrapper.  We have a chicken-before-the-egg
        /// scenario which is why we moved the Unity run logic out of the 
        /// BootStrapper constructor.  
        /// 
        /// The LoadMEFBootStrapper will allow MEF to Compose (resolve) it's 
        /// imports and exports which will result in the IUnityContainer being 
        /// set below.  When MEF sets it's value the BootStrapper is set with
        /// a reference to it.  Immediately following a successful MEF bootstrap 
        /// the Unity bootstrap starts using the MEF IUnityContainer reference.
        /// </summary>
        public MEFBootStrapper BootStrapper = new MEFBootStrapper();

        private IUnityContainer _unityContainer;  
        /// <summary>
        /// MEF is responsible for Composing (resolving) the IUnityContainer.
        /// The MEFUnity part (Parts folder) exports the applicable UnityContainer
        /// </summary>
        [Import(typeof(IUnityContainer))]
        public IUnityContainer Container { 
            get { return _unityContainer; }
            set
            {
                _unityContainer = value;
                BootStrapper.UnityContainer = value; 
            }
        }

        /// <summary>
        /// The Window1.xaml.cs will export "MainWindow" (a reference to itself)
        /// so that the application will have a reference to it
        /// </summary>
        [Import("MainWindow")]
        public new Window MainWindow
        {
            get { return base.MainWindow; }
            set { base.MainWindow = value; }
        }

        /// <summary>
        /// Start the MEF Bootstrapper
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            BootStrapper.LoadMEFBootStrapper(this,
                new BootStrapperEventArgs(Assembly.GetExecutingAssembly()));
        }

        /// <summary>
        /// Provide MEF the location of the parts; we'll place them in the
        /// bin\Extensions folder
        /// </summary>
        /// <param name="catalogs"></param>
        public void OnAddCatalog(ICollection<ComposablePartCatalog> catalogs)
        {
            catalogs.Add(
                new DirectoryPartCatalog("Extensions", false));
        }
    }
}

Last edited Oct 4, 2008 at 12:54 AM by BillKrat, version 1

Comments

No comments yet.