Managed Extensibility Framework(MEF) is an old concept of building extensible applications. But it was not a part of Silverlight3. To work on MEF we had to get the MEFPreview8(latest version) from Codeplex and then include the .dll files in our project,to implement it.
Silverlight 4 has got it inbuilt. This is great.So let us first understand to see what MEF is.
Managed Extensibility Framework revolves around three concepts.
Export: What you need to provide.
Import: Get what has been provided.
Compose: Do something with what you got.
Lets c it practically 🙂
I created a Silverlight application named MEF hosted in ASP.Net Web Application project .
Then I included another Silverlight Usercontrol . Named it as LocalControl.xaml.
To implement MEF,we have to add reference to :
System.ComponentModel.Composition.dll and System.ComponentModel.Composition.Initialization.dll
The logic goes this way:
I have to export usercontrol that I created in LocalControl.xaml file and import it in my MainPage.xaml.
Code added to LocalControl.xaml file:
<RichTextArea Width=”180″ Background=”Beige” Height=”180″ Margin=”10″ ></RichTextArea>
Code Behind in LocalControl.xaml.cs file.
|using System.ComponentModel.Composition;namespace MEF
public partial class LocalControl : UserControl
The Export attribute tells MEF that the LocalControl is available for service.
In MainPage.xaml to display this Control in Stackpanel on click event of button MEF concept has been used.
Code added to MainPage.xaml
xmlns:controls=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls”> <Grid x:Name=”LayoutRoot” Background=”White”>
<StackPanel Width=”250″ Height=”250″ x:Name=”pnl_Display” Background=”Brown”></StackPanel>
<Button x:Name=”btn_DisplayControl” Height=”30″ Width=”200″ Content=”Click to display control” Click=”btn_DisplayControl_Click”/>
Code behind in MainPage.xaml.cs
public partial class MainPage : UserControl
public UserControl myControl = new UserControl();
private void btn_DisplayControl_Click(object sender, RoutedEventArgs e)
If you observe the code in MainPage.xaml.cs while creating an object I have simply created a usercontrol object. It is nowhere specified that it is of type LocalControl. Still after I run my project I am able to see the LocalControl on click event of button.
So this is MEF’s magic.
The Import attribute gets whatever has been exported . We are right now exporting only one usercontrol.
If there are many exports, you have to use ImportMany attribute.
Once the imports are done,you have to tell our MainPage usercontrol to accept the imports.
PartInitializer.SatisfyImports(this) will do the job.
If this statement is not added you won’t see the LocalControl in StackPanel on click event of button.
Here are the snapshots:
StackPanel with RichtexArea control in LocalControl.xaml file
StackPanel where the LocalControl has to be displayed on clicked event of button in MainPage.xaml
This application is very very basic . But the power of MEF is much more.
I would be blogging more on this.. 🙂
The best links to get started :