UserControl with MEF Imports

Topics: Prism v4 - WPF 4
Mar 30, 2011 at 3:24 PM

I am writing a "Business" UserControl. What this means is that it is a re-usable control that has to contact a server for data. The inputs for the control are properties set from xaml, the outputs for the control are dependency properties that the containing view can bind values to in xaml.

I want the UserControl to be able to get references to the ExceptionHandlerService, DataService and it's own ViewModel via MEF Imports.

i.e. the xaml file for my view would contain the line:

<uc:MyObjectSearch Searchstring1="objectname" Searchstring2="{Binding ObjectMetaData}" Object="{Binding SelectedObject}" />

(where ObjectMetaData and SelectedObject are properties the views ViewModel, Object is a DP on the UserControl which gets set after some user interaction with the control).

However, this means that the object is constructed by XAML and so outside of the MEF container, which means none of the imports will be satisfied. What is the best solution for this?

I have found one method that ports the CompositionInitializer for Silverlight MEF
With this, you can call CompositionInitializer.SatisfyImports(this); after your InitializeComponent(); in your UserControl constructor and all property imports will be satisfied. (You also need a CompositionHost.Initialize(AggregateCatalog); in ConfigureAggregateCatalog in your bootstrapper.

Another method is to use an attached property as described here: where you can add a property to your xaml definition like so:

<uc:MyObjectSearch Searchstring1="objectname" Searchstring2="{Binding ObjectMetaData}" Object="{Binding SelectedObject}" mef:Composition.Compose="True" />

But this method involves some magic in the App.xaml:

            <mef:DirectoryCatalog Directory=".\Extensions"/>

And so i'm not really sure how this will fit into a prism scenario. If it does at all...

Finally, in the previous post a MEF developer describes a method that they are planning on implementing so that you don't have to do anything and the MEF container will intercept xaml object constructions and inject where necessary. But there have been no replies since (I have posted to ask).

So, what would you suggest for this? My plan is to use CompositionInitializer, but this isn't ideal as obviously I can't use contructor injection.


Mar 30, 2011 at 5:48 PM


In the context of the scenario you're describing, and based on my understanding, I would recommend to use CompositionInitializer, unless you can avoid adding the lines you've mentioned in the App.xaml. While adding the lines to the App.xaml that shouldn't cause problems with the use of MEF in Prism, it's not the usual way and could pose additional difficulties.

I hope you find this helpful.

Guido Leandro Maliandi

Mar 30, 2011 at 10:42 PM

Great to hear a "thumbs up" (much appreciated Guido), but I would definitely be interested in other ideas too. If anyone has an alternate way of solving my problem (I have hacked it in the past by injecting a "control vm" into the container and passing it to the control from the containers code-behind) then that would certainly be interesting.

Apr 11, 2011 at 2:23 PM

The CompositionInitializer idea is not bad - but a bit too much code for an existing application.
I would just expose your MEF Container SatisfyImports method threw a static class (which you will need to init in your bootstrapper).

Apr 11, 2011 at 3:02 PM

thebond: As I understand it, that is exactly what the CompositionHost/CompositionInitializer achieves. I think the advantage over doing this rather than writing your own static class is that you are following a pre-existing MEF pattern which could find it's way into the core MEF libraries.

The link I gave provides a simple project that you can compile and include in your shell and any modules that need to use it (something you would have to do if you were writing your own static class anyway). The amount of code should be the same (one line to init in your bootstrapper and then a call to SatisfyImports wherever needed).