Search code examples
c#wpfmefcaliburn.micro

Caliburn micro cant find MainView when using mef


I am currently a bit clueless. I am using Caliburn micro framework with MEF. The main application should load plugins which contain basically only service functionality. But as soon as I reference an external assembly CaliburnMicro refuses to find the MainWindowView (which works well if no external assembly is referenced).

My Bootstrapper (basically the one from the CM example):

public class AppBootstrapper : BootstrapperBase
{
    private CompositionContainer container;

    public AppBootstrapper()
    {
        Initialize();
    }

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

        var batch = new CompositionBatch();

        batch.AddExportedValue<IWindowManager>( new WindowManager() );
        batch.AddExportedValue<IEventAggregator>( new EventAggregator() );
        batch.AddExportedValue( container );

        container.Compose( batch );
    }

    protected override object GetInstance( Type serviceType, string key )
    {
        var contract = string.IsNullOrEmpty( key ) ? AttributedModelServices.GetContractName( serviceType ) : key;
        var exports = container.GetExportedValues<object>( contract );

        if ( exports.Any() )
            return exports.First();

        throw new Exception( $"Could not locate any instances of contract {contract}." );
    }

    protected override IEnumerable<object> GetAllInstances( Type serviceType )
    {
        return container.GetExportedValues<object>( AttributedModelServices.GetContractName( serviceType ) );
    }

    protected override void BuildUp( object instance )
    {
        container.SatisfyImportsOnce( instance );
    }

    protected override void OnStartup( object sender, StartupEventArgs e )
    {
        base.OnStartup( sender, e );
        DisplayRootViewFor<IMainWindow>();
    }
}

My MainWindow

[Export( typeof( IMainWindow ) )]
public class MainWindowViewModel : Conductor<IShell>, IMainWindow, IPartImportsSatisfiedNotification
{
    private string _title = "Title";

    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
            NotifyOfPropertyChange( () => Title );
        }
    }

    private ImageSource _icon;

    public ImageSource Icon
    {
        get { return _icon; }
        set
        {
            _icon = value;
            NotifyOfPropertyChange( () => Icon );
        }
    }

    [Import]
    private IResourceManager _resourceManager;

    [Import]
    private IShell _shell;

    public IShell Shell
    {
        get { return _shell; }
        private set
        {
            _shell = value;
            NotifyOfPropertyChange( () => Shell );
        }
    }

    public void OnImportsSatisfied()
    {
        if ( Icon == null )
            Icon = _resourceManager.GetBitmap( "Resources/Icons/icon.ico" );
    }
}

The Shell and the ResourceManager are in the main assembly/executable (same as the MainWindow/Bootstrapper).

I tried to export an interface from an external Assembly and import it with the ShellViewModel.

My ShellViewModel

[Export( typeof ( IShell ) )]
public class ShellViewModel : PropertyChangedBase, IShell
{
    [Import]
    private ITestInterface _testInterface;

    public ITestInterface TestInterface
    {
        get { return _testInterface; }
        set { _testInterface = value; }
    }
}

My external assembly:

public interface ITestInterface
{
    string Test();
}

[Export(typeof(ITestInterface))]
public class MyTest : ITestInterface
{
    public string Test()
    {
        return "Test ok";
    }
}

Solution

  • Solution found:

            protected override void Configure()
        {
            container = new CompositionContainer( new ApplicationCatalog() );
    
            var batch = new CompositionBatch();
    
            batch.AddExportedValue<IWindowManager>( new WindowManager() );
            batch.AddExportedValue<IEventAggregator>( new EventAggregator() );
            batch.AddExportedValue( container );
    
            container.Compose( batch );
        }
    

    Changing AggregateCatalog to ApplicationCatalogsolved the problem.