Caliburn Micro: MEF Script Plugin Issues

I have a script with a WPF application that contains some views (user controls) with their view modes, found as exported MEF parts in its plugin folder. The application downloads its data along with the configuration file, which also indicates which parts should be imported from the available ones.

The first question is related to the MEF loader: how to configure it so that it knows about the plugins folder? I know that its SelectAssemblies overrides it, but this requires assemblies, while my typical approach would be the MEF directory directory. I would not want to use methods such as Assembly.LoadFrom for every DLL found in the directory: MEF exists for this purpose (lifecycle management, etc.). So, how can I do something like the MEF AggregateCatalog created from the DirectoryCatalog directory in the boot file?

The second question : as soon as I have a list of virtual machines that I need, I want to create them. Some of them require injection of CM services, such as IEventAggregator or IWindowManager, so they have a corresponding importer constructor, and therefore I need CM to create them for me: but I need to do this programmatically, so I can’t just use the import attribute of the property or importing constructor.

The same is true for views: as soon as I receive my virtual machines, I need CM to create them and set the corresponding virtual machine as my data context; but I cannot use the window manager because I just want to get them, and then programmatically add them (they are user controls) to the tab control, which is composed differently according to the data configuration.

I use MEF because the application is plugin-centric, and therefore I can adhere to its limitations when used as an IoC. But I would like to use CM to create instances and view modes (all of them were contained in several plug-in DLL files) and correctly link them. Can someone give some hints or point out samples or documentation about this?

, :):

:

  • ( MEF) . - MEF, .

  • , :

static private object LocateViewFor(object viewmodel)
{
  UIElement view = ViewLocator.LocateForModel(viewmodel, null, null);
    ViewModelBinder.Bind(viewmodel, view, null);
  return view;
}

CM, . "" MefBootstrapper, WPF (., , ). , null.

, MEF. , CM, MEF DirectoryCatalog . bootstrappers CM - SelectAssemblies, , Assembly. . , - , :

private IEnumerable GetDirectoryCatalogs()
{
 return new ComposablePartCatalog[]
 {
 new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory)
 // TODO other plugins folders...
  };
}

, :

_container = new CompositionContainer(
  new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType()
  .Union(GetDirectoryCatalogs())));

MEF, , "" CM AssemblySource.Instance: infact, VM MEF, CM ( , IWindowManager.ShowDialog(myviewmodel, null)) null ( ) "placeholder", , .

, , , , viewmodel, , MEF. , , viewmodel SomeNamespace.SampleViewModel - SomeNamespace.SampleView; , , , [Export] . , CM , , . MEF , , CM , "" .

?

+3
1

Caliburn Micro , CompositionInitializer ? , CompositionHost , , :

var catalog = new AggregateCatalog(
    new DirectoryCatalog("bin"),
    new DirectoryCatalog("Plugins"));

CompositionHost.Initialise(catalog);

CompositionInitialiser.SatisfyImports(...) , .

, , CompositionContainer. . , :

var catalog = new AggregateCatalog(
    new DirectoryCatalog("bin"),
    new DirectoryCatalog("Plugins"));
var container = new CompositionContainer(catalog);
CompositionHost.Initialise(container);

Container = container;

... Container - CompositionContainer. CompositionHost , CompositionInitializer, , , , :

var viewModel = Container.GetExport<ISomeViewModel>();

?

+1

All Articles