(I resurrect this in case anyone encounters this. As an expression of disclaimer, I am not an expert, and these are just solutions that I found working.)
I found that calling CompositionContainer.ComposeParts(myUserControl) works. I call this on the management constructor. You need to somehow get a link to the CompositionContainer:
public MyUserControl() { compositionContainer.ComposeParts(this); }
Additional solution:
This is probably unnecessary, but here is another way. This is much more confusing, but allows you to " Import " control the user in XAML.
For your import to be satisfied, MyUserControl needs to be exported and then an instance of MEF created. My solution was to have a static field in the class that contains the Locator object. This locator object is responsible for importing and returning exported objects. Then I could refer to this static field in XAML, like so:
<ContentControl Content="{Binding MyUserControl, Source={x:Static v:SomeClass.Locator}}">
SomeClass has a static property called Locator , which is assigned early in the application life cycle. The locator can have the MyUserControl property, which gets Import ed.
(Disclaimer: The links below refer to my own structure, and a solution as rude as it is should be used with caution.)
To give an example of the above, I will explain how I implemented it in my environment:
In my case, SomeClass is a subclass of System.Windows.Application that replaces App.xaml , and the ViewLocator is assigned to its OnStartup , as can be seen here .
The ViewLocator class is a System.Dynamic.DynamicObject that imports views that have their own ViewExport attribute. Views are identified using the ViewExportAttribute.Alias property.
This is an example of an export that is assigned an alias.
Finally, an instance of the MEF view instance can be used in XAML as follows:
<ContentControl Content="{Binding HomeView, Source={x:Static v:FrameworkApp.ViewLocator}}">