Bagikan melalui


CompositionInitializer Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Provides static access to methods for parts to satisfy imports.

Inheritance Hierarchy

System.Object
  System.ComponentModel.Composition.CompositionInitializer

Namespace:  System.ComponentModel.Composition
Assembly:  System.ComponentModel.Composition.Initialization (in System.ComponentModel.Composition.Initialization.dll)

Syntax

'Declaration
Public NotInheritable Class CompositionInitializer
public static class CompositionInitializer

Methods

  Name Description
Public methodStatic member SatisfyImports(ComposablePart) Fills the imports of the specified part.
Public methodStatic member SatisfyImports(Object) Fills the imports of the specified attributed part.

Top

Remarks

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.

Examples

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.

Partial Public Class MainPage
    Inherits UserControl

    <Import()>
    Public Property addIn As MyAddIn

    Public Sub New()
        InitializeComponent()
        CompositionInitializer.SatisfyImports(Me)
        TextBox1.Text = addIn.getTheData()

    End Sub

End Class

<Export()>
Public Class MyAddIn

    <Import()>
    Public Property subOne As MySubOne

    <Import()>
    Public Property subTwo As MySubTwo

    Public Function getTheData() As String
        '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
    End Function

End Class

<Export()>
Public Class MySubOne
    Public data = "Sub One String!"
End Class

<Export()>
Public Class MySubTwo
    Public data = "Sub Two String!"
End Class
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!";
}

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.