共用方式為


CompositionContainer.Compose(CompositionBatch) 方法

定義

在容器中加入或移除指定之 CompositionBatch 的組件,然後執行撰寫。

public:
 void Compose(System::ComponentModel::Composition::Hosting::CompositionBatch ^ batch);
public void Compose (System.ComponentModel.Composition.Hosting.CompositionBatch batch);
member this.Compose : System.ComponentModel.Composition.Hosting.CompositionBatch -> unit
Public Sub Compose (batch As CompositionBatch)

參數

batch
CompositionBatch

變更 CompositionContainer 以在撰寫期間納入。

範例

在這個簡單的範例中,會建立三個部分並新增至 CompositionContainer,並擷取一個元件,以顯示已填入所有匯入。 此範例使用 Attributed 程式設計模型。

[Export]
class Part1
{
    public String data = "This is the example data!";
}

[Export]
class Part2
{
    [Import]
    public Part1 data { get; set; }
}

[Export]
class Part3
{
    [Import]
    public Part2 data { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        CompositionContainer container = new CompositionContainer();
        CompositionBatch batch = new CompositionBatch();
        batch.AddPart(AttributedModelServices.CreatePart(new Part1()));
        batch.AddPart(AttributedModelServices.CreatePart(new Part2()));
        batch.AddPart(AttributedModelServices.CreatePart(new Part3()));
        container.Compose(batch);
        Part3 _part = container.GetExportedValue<Part3>();
        Console.WriteLine(_part.data.data.data);
        Console.ReadLine();
    }
}
<Export()>
Public Class Part1
    Public ReadOnly Property data As String
        Get
            Return "This is the example data!"
        End Get
    End Property
End Class
<Export()>
Public Class Part2
    <Import()>
    Public Property data As Part1
End Class

<Export()>
Public Class Part3
    <Import()>
    Public Property data As Part2
End Class


Sub Main()
    Dim container As New CompositionContainer()
    Dim batch As New CompositionBatch()
    batch.AddPart(AttributedModelServices.CreatePart(New Part1()))
    batch.AddPart(AttributedModelServices.CreatePart(New Part2()))
    batch.AddPart(AttributedModelServices.CreatePart(New Part3()))
    container.Compose(batch)
    Dim _part As Part3
    _part = container.GetExportedValue(Of Part3)()
    Console.WriteLine(_part.data.data.data)
    Console.ReadLine()

End Sub

備註

此方法是直接新增或移除容器中元件的主要方式。 一 CompositionContainer 律會維持穩定且組合的狀態。 因此,永遠不需要以空白CompositionBatch呼叫Compose,才能開始撰寫。 相反地,每當您需要對 可用的CompositionContainer元件進行變更時,請呼叫 Compose 方法。

CompositionBatch可以包含要加入的元件和要移除的元件。 每次呼叫 Compose時,只會進行一次重新組合。

適用於

另請參閱