MEF - WPF QuickStart

Currently the Managed Extensibility Framework (MEF) does not have documentation/steps for creating a new WPF Application (there is documentation for a console application) so I figured I'd document the steps I take here; there are plenty of samples to get me going.

  1. Create New WPF Application
  2. Remove the StartupUri="Window1.xaml" from the App.xaml file
  3. Add a reference to the Projects | ComponentModel project
  4. Add the following code to the App.xaml.cs code-behind file:

        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            private CompositionContainer _container;

            [Import("MainWindow")]
            public new Window MainWindow
            {
                get { return base.MainWindow; }
                set { base.MainWindow = value; }
            }

            protected override void OnStartup(StartupEventArgs e)
            {
                base.OnStartup(e);

                if (Compose())
                {
                    MainWindow.Show();
                }
                else
                {
                    Shutdown();
                }
            }
            protected override void OnExit(ExitEventArgs e)
            {
                base.OnExit(e);

                if (_container != null)
                {
                    _container.Dispose();
                }
            }
            private bool Compose()
            {
                var catalog = new AggregatingComposablePartCatalog();
                catalog.Catalogs.Add(new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly()));

                _container = new CompositionContainer(catalog.CreateResolver());
                _container.AddPart(this);

                try
                {
                    _container.Compose();
                }
                catch (CompositionException compositionException)
                {
                    MessageBox.Show(compositionException.ToString());
                    return false;
                }
                return true;
            }

        }
     

  5. Add an [Export("MainWindow")] attribute to the Window1.xaml.cs file's Window1 class.



  6. Compile and run the application - your Window1.xaml file should load.  If you get two windows then you failed to comply with step #2 above

  
A less conventional route is to use a "BootStrapper" class

The BootStrapper code follows:

namespace MEFContrib.Library.Base
{
    public class BootStrapper
    {
        private Assembly _assembly;
        private Application _application;
        private CompositionContainer _container;

        public BootStrapper(object sender, BootStrapperEventArgs e)
        {
            _application = sender as Application;
            _assembly = e.Assembly;

            _application.Exit += new ExitEventHandler(_application_Exit);

            if (Compose())
                _application.MainWindow.Show();
            else
                _application.Shutdown();
        }

        void _application_Exit(object sender, ExitEventArgs e)
        {
            if (_container != null)
                _container.Dispose();
        }

        private bool Compose()
        {
            var catalog = new AggregatingComposablePartCatalog();
            catalog.Catalogs.Add(new AttributedAssemblyPartCatalog(_assembly));
            ((IMefApplication)_application).OnAddCatalog(catalog.Catalogs);

            _container = new CompositionContainer(catalog.CreateResolver());
            _container.AddPart(_application);

            try
            {
                _container.Compose();
            }
            catch (CompositionException compositionException)
            {
                MessageBox.Show(compositionException.ToString());
                return false;
            }
            return true;
        }
    }
}

MEF_QuickStart.zip (2.10 mb)


Tags:
Categories: MEF


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know