Tags: Blog |
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

Add a new application to create a new .XAP file

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

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
f87325cc-ada4-4b08-8710-f80d290e23a8|0|.0