Importing in custom controls

Topics: Bootstrappers & IoC
Jul 27, 2011 at 10: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 28, 2011 at 12:53 AM

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 6: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 12:46 PM

Is this WPF or Silverlight?

Jul 28, 2011 at 1:54 PM

Sorry, WPF.