MefBootstrapper with WPF

Jul 31, 2010 at 6:04 PM

Hi

I completed part 1 of the tutorial and posted my observations on this thread.

Now I am on to part 2  i.e. to use Mef with WPF4, I added the MefBootstrapper and based on the part 2 tutorial, I did the following changes specific to WPF4

(1) Added just the System.ComponentModel.Composition reference
(2) Renamed HelloBootstrapper to MefBoostrapper and changed the name in App.xaml reference to <local:MefBootstrapper x:Key="bootstrapper" />.  I modified the Configure method.  See below

namespace Caliburn.Micro.WPFHello
{
    public class MefBootstrapper : Caliburn.Micro.Bootstrapper
    {
        private CompositionContainer container;

        protected override void Configure()
        {
            container = new CompositionContainer();
        }
.....  (rest of the code is the same)

Now I can't get the WPF ShellView to showup.
What I am I missing?
Thanks
rockmeister

 

Jul 31, 2010 at 8:54 PM

Sigh...  Never mind...  I went back and actually read the whole part 2  :-)  Now the HelloWPF app is working fine with MEF.  For those who are interested here is the relevant part of the MefBoostrapper for WPF4

namespace Caliburn.Micro.WPFHello
{
    class MefBootstrapper : Caliburn.Micro.Bootstrapper<IShell>
    {
        private CompositionContainer container;

        protected override void Configure()
        {
            container = new CompositionContainer(new AggregateCatalog(
                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
                ));
        }

Rest of the code is the same.

 

BTW, you still have to create an marker IShell interface and do the following changes to the ShellViewModel class

namespace Caliburn.Micro.WPFHello
{  
    using System.Windows;
    using System.ComponentModel.Composition;

    [Export(typeof(IShell))]
    public class ShellViewModel : PropertyChangedBase, IShell  
    {

 

I kind of miss the conventions based approach of wiring ShellViewModel to ShellView as described in part 1.  In the MEF based approach it looks like these need to be done via code.  I was planning to use MEF for the ability to do plugins.  Maybe I shouldn't use MEF as the IOC in Caliburn.Micro and use MEF separately to detect plugins.  I hope the question makes sense...

Thanks
rockmeister

 

 

 

Aug 3, 2010 at 2:47 PM
As far as I can tell, the IShell is being used primarily as a sentinal to identify the root viewmodel and to distinquish this PropertyChangedBase derived class as a viewmodel. It might be possible to create another export attribute that accomplishes the same thing with another parameter. I have been toying with an autofac based bootstrapper that uses the convention of registering as a view model any type ending with 'ViewModel', and treating a type titled RootViewModel as the root view model. I have not figured out how to move that logic into MEF yet, but if I do, that might meet your needs. David