views:

35

answers:

2

What is the proper way to do that? Let's say we have some third party library in our project and we need to provide an access to some controls which are sealed. New widgets will be created and added to the application using MEF and they should be able to import some controls from the core application. So how to export those controls properly?

A: 

What about wrapping the third party controls in "export" classes and then access this control through the wrapper?

blindmeis
This is actually the I'm doing it at the moment. But I lost the Blendability since I cannot add the exported control in Xaml anymore so it doesn't seem the right way to do it. I need to share single instance of the control everywhere so now even where it already is accessible (e.g. in Main page) I have to use the import mechanism and add it to some container in code behind.
incognito
A: 

If you cannot modify the original class (e.g. ThirdPartyComponent), then you can do the export via a property on another class (e.g. ThirdPartyComponentExporter):

public class ThirdPartyComponentExporter
{
   [Export(typeof(ThirdPartyComponent))]
   public ThirdPartyComponent Foo
   {
      get
      {
         return new ThirdPartyComponent();
      }
   }
}

For visual controls, you may have to use CreationPolicy.NonShared to prevent MEF from reusing the same instance in different locations.

Wim Coenen
Is Shared the default creation policy? I tried something similar and added the control i wanted to export inside a user control and exposed it via property. Then I added that user control to the main page (since I wanted to see the exported control in visual designer and that was the only way I managed to do it). And it looked like two different instances of exportable control were being created, one by MEF and a different one because I added a user control containing the exportable one to the page.
incognito
yes shared is the default creation policy.
blindmeis
@Incognito: By default, MEF will try to reuse the instances which it creates itself. But it will not reuse instances that *you* created, as you describe here. It doesn't know about those instances.
Wim Coenen
Could you suggest how to add that exported control to the page in xaml instead of adding it in the code behind as a child of some other container. I was thinking about something like a placeholder for the control in xaml. I have already asked about that in other post http://stackoverflow.com/questions/4025197/placeholder-for-a-control-in-silverlight
incognito