Importing in custom controls

Topics: Bootstrappers & IoC
Jul 27, 2011 at 9:18 PM

Not sure who to ask... I think this is a general MEF problem, but I'll ask here in hopes that someone's encountered it before:

If I have a custom control with an import in it, how can I get the imports composed?


public class ImportingButton : Button

   public object ImportedContent{ set { this.Content = value; } }

and in view's xaml...




I've been playing around and if I do the same import the parameter into a view model, it's fine.

Any idea's?

Jul 27, 2011 at 11:53 PM

Since the controls are created by the Xaml parser, they aren't natively tied into MEF, thus their imports will not be satisfied. I believe the strategy for handling this is to call container.SatisfyImportsOnce(this) in the control's ctor after the call to InitializeComponent. I've never done this before, so I'm going off my memory of blog posts I read a while back...

Jul 28, 2011 at 5:08 AM

Ahhh,  now I get it.

So, if the control is in library, I'm assuming that a workaround would likely be to by modifying the boostrapper code then?

Jul 28, 2011 at 11:46 AM

Is this WPF or Silverlight?

Jul 28, 2011 at 12:54 PM

Sorry, WPF.