Tags: | Categories: Blog Posted by admin on 9/10/2009 2:57 PM | Comments (3)

Tonight I wanted to play a little with the new Managed Extensibility Framework MEF for short library.

A few simple steps to start with MEF.

Download the Managed Extensibility Framework from here

Add a reference to the MEF dll System.ComponentModel.Composition.dll found after unzipping the download at the MEF_Preview_6\bin\SL3

image

Add a new application to create a new .XAP file

image

Uncheck the box to link it to .NET RIA Services if its installed in your computer yet keep the link to the Web app without creating the test page. Delete the MainPage.xaml as well as the App.xaml

Add a new UserControl

image

Add these code to add dynamically the xap file to pull it the app, make sure the new xap file is on the ClientBin directory on the Web tier. Otherwise you’ll have to modify the call to be an absolute path.

var catalog = new PackageCatalog();
          catalog.AddPackage(Package.Current);
          Package.DownloadPackageAsync(new Uri("NewXapFile.xap", UriKind.Relative), (s, p) => catalog.AddPackage(p));

          var container = new CompositionContainer(catalog);
          container.ComposeParts(this);

I created the interface and loaded the xap.

Now for the contract to work I had to create a interface and set it like:

[InheritedExport]
    public interface IMyInterface

 

Inported with a collection

[ImportMany(AllowRecomposition = true)]
       public ObservableCollection<IMyInterfacet> MyInterface { get; set; }

 

And download it with using it after completed

Package.DownloadPackageAsync(new Uri("My.xap", UriKind.Relative), (s, p) =>
           {
               catalog.AddPackage(p);
               LayoutRoot.Children.Add(MyInterface[0].GetControl());
           });

And now you can use the methods inside the user control, happy MEFin’ 

I have many questions after the first application, even that everything works, the piping of the attributes are a little complicated, thanks to Brad Abrams post to help me, yet I have try to avoid using the collection to import the XAP UserControl without any success. I cannot find the reason why we need a collection to import one XAP file class.
Also tomorrow I can see that I would like to play with .XAP files hosted in different servers, not just in the ClientBin directory, so you can use the absolute URL to download the file and run it on your browser, making it more powerful to release different packages around the web, as long as you have the contract or interface.

Cheers

Al

Follow me in Twitter for updates

Comments

on 9/12/2009 5:29 AM

Hi Al


Nice post. The reason you need an ObservableCollection is so the UI updates when recomposition happens. There's no ObservableValue in the framework. An additional reason is that for imports like this there tends to be more than one exporter.  


If you are sure there will only ever be one, you could remove the collection and make your class implement INotifyPropertyChanged, and then import the single value with AllowRecomposition set to true. In the setter of the property you would then need to throw the PropertyChanged event to notify the UI.


HTH


Glenn


on 10/24/2009 5:41 PM

hello Al


I'm very clumsy, i need how implement this code with the Interface


MyInterface[0].GetControl()); ... how find the control of the other .xap


thanks, and sorry, I clumsy.

on 11/17/2009 10:28 PM

thanks for sharing this

Comments are closed