Condividi tramite


CompositionHost Class

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

Provides static methods to control the container used by CompositionInitializer.

Inheritance Hierarchy

System.Object
  System.ComponentModel.Composition.Hosting.CompositionHost

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

Syntax

'Declaration
Public NotInheritable Class CompositionHost
public static class CompositionHost

Methods

  Name Description
Public methodStatic member Initialize(array<ComposablePartCatalog[]) Sets CompositionInitializer to use a new container initialized with the specified catalogs.
Public methodStatic member Initialize(CompositionContainer) Sets CompositionInitializer to use the specified container.

Top

Remarks

By default, calls to SatisfyImports use a default container initialized with all the parts available in the application. By calling the Initialize method, you can override that default container in two ways.

First, calling Initialize with a set of catalogs creates a new container initialized with those catalogs. This allows the developer more control over which parts are included in the container, and can be useful for application partitioning.

Second, calling Initialize with a container sets that container to be used with CompositionInitializer, giving the developer complete control over the container's behavior.

Examples

The following code creates a catalog from the current assembly and sets it for use by CompositionInitializer.

Partial Public Class MainPage
    Inherits UserControl

    <Import()>
    Public Property addIn As MyAddIn

    Public Sub New()
        InitializeComponent()
        Dim catalog As AggregateCatalog = New AggregateCatalog()
        catalog.Catalogs.Add(New AssemblyCatalog(Me.GetType().Assembly))

        'Override the default host with one created with this catalog.
        CompositionHost.Initialize(catalog)

        CompositionInitializer.SatisfyImports(Me)
        MessageBox.Show(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();
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(App).Assembly));

        //Override the default host with one created with this catalog.
        CompositionHost.Initialize(catalog);

        CompositionInitializer.SatisfyImports(this);
        MessageBox.Show(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.