Let's look at a very simple scenario in which MEF can be used to “inject” an instance of a class into another class, without the need to explicitly “new up” the class instance. We'll make the class known to MEF by “exporting” it, and “import” it into the view's code-behind class so that it can use it.
Chapter14Sample
.System.ComponentModel.Composition.dll
System.ComponentModel.Composition.Initialization.dll
PersonPart.cs
, containing the following code:
namespace Chapter14Sample
{
public class PersonPart
{
public string Name { get; set; }
public PersonPart()
{
Name = "Homer Simpson";
}
}
}
Essentially, we've just added a property named Name
to the class, and assigned it a value in the class's constructor. We'll use that value to demonstrate that an instance of this class has been successfully imported shortly.
using
statement to the top of the class's file:
using System.ComponentModel.Composition;
Export
attribute, like so:
[Export]
public class PersonPart
The completed code for the PersonPart
class that you should now have is as follows:
using System.ComponentModel.Composition;
namespace Chapter14Sample
{
[Export]
public class PersonPart
{
public string Name { get; set; }
public PersonPart()
{
Name = "Homer Simpson";
}
}
}
MainPage.xaml
view. Let's “import” the PersonPart
class that we just exported into this view, and display the value assigned to its Name
property. Start by adding the following using
statement to the top of the MainPage.xaml.cs
file:
using System.ComponentModel.Composition;
MainPage
class that MEF will inject a PersonPart
object into, like so:
public PersonPart MyImportedPersonPart { get; set; }
PersonPart
class to this property for us by decorating the property with MEF's Import
attribute, as follows:
[Import]
public PersonPart MyImportedPersonPart { get; set; }
PersonPart
class and assign it to this property. You do so using the SatisfyImports
method on the CompositionInitializer
class. Add the following line of code to the MainPage
class's constructor:
CompositionInitializer.SatisfyImports(this);
This will result in the parts being “composed” by MEF when the MainPage
class is initialized, with an instance of the PersonPart
class being assigned to the MyImportedPersonPart
property.
PersonPart
part has been successfully imported, let's display the value that we assigned to its Name
property in its constructor in the view. Add the following code in bold to the MainPage
class's constructor:
public MainPage()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
var nameTextBlock = new TextBlock();
nameTextBlock.Text = MyImportedPersonPart.Name;
LayoutRoot.Children.Add(nameTextBlock);
}
This code adds a TextBlock
control to the view, and displays the value assigned to the PersonPart
part's Name
property in the view. The complete code that you should now have for the MainPage
class is as follows:
using System.ComponentModel.Composition;
using System.Windows.Controls;
namespace Chapter14Sample
{
public partial class MainPage : UserControl
{
[Import]
public PersonPart MyImportedPersonPart { get; set; }
public MainPage()
{
InitializeComponent();
CompositionInitializer.SatisfyImports(this);
var nameTextBlock = new TextBlock();
nameTextBlock.Text = MyImportedPersonPart.Name;
LayoutRoot.Children.Add(nameTextBlock);
}
}
}