Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

New Post: Interfaces are resolved only once with MEF

$
0
0
We have set a Bootstrapper of Caliburn.Micro as below:
public class Bootstrapper : Bootstrapper<ShellViewModel> {
    private CompositionContainer container;

    protected override void Configure() {
        if (Execute.InDesignMode)
            return;

        IEnumerable<AssemblyCatalog> assemblyCatalogs = AssemblySource.Instance.Select(x => new AssemblyCatalog(x));
        AggregateCatalog aggregateCatalog = new AggregateCatalog(Enumerable.OfType<ComposablePartCatalog>(assemblyCatalogs));
        container = new CompositionContainer(aggregateCatalog);

        CompositionBatch batch = new CompositionBatch();

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

        container.Compose(batch);            
    }
}

Here is ShellViewModel
[Export(typeof(ShellViewModel))]
public class ShellViewModel : Conductor<object>, IHandle<ChangePageMessage>, IHandle<OpenWindowMessage> {

[Import]
public IEventAggregator EventAggregator { get; private set; }

[Import]
public IWindowManager WindowManager { get; private set; }

public void Handle(ChangePageMessage message) {
  ActivateItem(message.ViewModel);
}

public void Handle(OpenWindowMessage message) {
  WindowManager.ShowWindow(message.ViewModel);
}

protected override void OnViewLoaded(object view) {
  EventAggregator.Subscribe(this);
  var vm = new DestinationChoiceViewModel();
  ActivateItem(vm);
}
}

IWindowManager and IEventAggregator are resolved in ShellViewModel.

But when DestinationChoiceViewModel loaded it's IWindowManager and IEventAggregator fields are null.

Here is a sample of DestinationChoiceViewModel:
[Export(typeof (DestinationChoiceViewModel))]
public class DestinationChoiceViewModel : Screen {
    private readonly DestinationChoiceModel model;

    [Import]
    public IEventAggregator EventAggregator { get; private set; }

    [Import]
    public IWindowManager WindowManager { get; private set; }

    public DestinationChoiceViewModel() {                     
        model = new DestinationChoiceModel();         
    }
So why are they resolved only in the first ViewModel? And how to force them to be resolved in other ViewModels?

Viewing all articles
Browse latest Browse all 265

Trending Articles