CompositionInitializer Class
Provides static access to methods for parts to satisfy imports.
Namespace: System.ComponentModel.Composition
Assembly: System.ComponentModel.Composition.Initialization (in System.ComponentModel.Composition.Initialization.dll)
| Name | Description | |
|---|---|---|
|
SatisfyImports(ComposablePart) | Fills the imports of the specified part. |
|
SatisfyImports(Object) | Fills the imports of the specified attributed part. |
This class is designed to allow parts to fill imports without the need to go through container initialization. Calls to the methods of this class are filled by using a default container loaded with all the parts found in the XAP file.
There are a few limitations on CompositionInitializer:
-
Parts passed to SatisfyImports for composition cannot have exports of their own, or the method will result in an exception.
-
Parts created by the default container to fill imports will be retained by the Managed Extensibility Framework (MEF) until the application shuts down. Long-running applications should using ExportFactory<T> to manage the life cycles of their parts.
You can replace the default container with a custom container by calling the Initialize static method.
The following code shows how to satisfy the imports for a part and get data from the part. To use this code, create a Silverlight project with a TextBox named textBox1. Add the following code to the code-behind file.
public partial class MainPage : UserControl { [Import] public MyAddIn addIn { get; set; } public MainPage() { InitializeComponent(); CompositionInitializer.SatisfyImports(this); textBox1.Text = addIn.getTheData(); } } [Export] public class MyAddIn { [Import] public MySubOne subOne { get; set; } [Import] public MySubTwo subTwo { get; set; } public String getTheData() { //This is safe, because MEF guarantees this //object will never be created with null references! //If it can't fill those imports, composition //will fail. return subOne.data + " " + subTwo.data; } } [Export] public class MySubOne { public String data = "Sub One String!"; } [Export] public class MySubTwo { public String data = "Sub Two String!"; }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
- 2/21/2011
- Pat Kujawa