Rediger

Del via


WorkflowDesignerLoader.PerformLoad(IDesignerSerializationManager) Method

Definition

Loads the designer from a design document.

protected:
 override void PerformLoad(System::ComponentModel::Design::Serialization::IDesignerSerializationManager ^ serializationManager);
protected override void PerformLoad (System.ComponentModel.Design.Serialization.IDesignerSerializationManager serializationManager);
override this.PerformLoad : System.ComponentModel.Design.Serialization.IDesignerSerializationManager -> unit
Protected Overrides Sub PerformLoad (serializationManager As IDesignerSerializationManager)

Parameters

serializationManager
IDesignerSerializationManager

Class that implements the IDesignerSerializationManager interface, which manages design-time serialization.

Examples

The following example shows how to override the PerformLoad method to deserialize a workflow and load it in the designer using the AddObjectGraphToDesignerHost helper method.

protected override void PerformLoad(IDesignerSerializationManager serializationManager)
{
    IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
    Activity rootActivity = null;

    // If a WorkflowType already exists, create an instance instead of reading from file
    if (WorkflowType != null)
    {
        rootActivity = (Activity)Activator.CreateInstance(WorkflowType);
    }
    else
    {
        // Create a text reader out of the doc data
        TextReader reader = new StringReader(this.xoml);
        try
        {
            using (XmlReader xmlReader = XmlReader.Create(reader))
            {
                WorkflowMarkupSerializer xomlSerializer = new WorkflowMarkupSerializer();
                rootActivity = xomlSerializer.Deserialize(xmlReader) as Activity;
            }
        }
        finally
        {
            reader.Close();
        }
    }

    if (rootActivity != null && designerHost != null)
    {
        AddObjectGraphToDesignerHost(designerHost, rootActivity);
    }
}

private static void AddObjectGraphToDesignerHost(IDesignerHost designerHost, Activity activity)
{
    Guid Definitions_Class = new Guid("3FA84B23-B15B-4161-8EB8-37A54EFEEFC7");

    if (designerHost == null)
        throw new ArgumentNullException("designerHost");
    if (activity == null)
        throw new ArgumentNullException("activity");

    string rootSiteName = activity.QualifiedName;
    if (activity.Parent == null)
    {
        string fullClassName = activity.UserData[Definitions_Class] as string;
        if (fullClassName == null)
            fullClassName = activity.GetType().FullName;
        rootSiteName = (fullClassName.LastIndexOf('.') != -1) ? fullClassName.Substring(fullClassName.LastIndexOf('.') + 1) : fullClassName;
        designerHost.Container.Add(activity, rootSiteName);
    }
    else
    {
        designerHost.Container.Add(activity, activity.QualifiedName);
    }

    if (activity is CompositeActivity)
    {
        foreach (Activity activity2 in GetNestedActivities(activity as CompositeActivity))
            designerHost.Container.Add(activity2, activity2.QualifiedName);
    }
}

private static Activity[] GetNestedActivities(CompositeActivity compositeActivity)
{
    if (compositeActivity == null)
        throw new ArgumentNullException("compositeActivity");

    IList<Activity> childActivities = null;
    ArrayList nestedActivities = new ArrayList();
    Queue compositeActivities = new Queue();
    compositeActivities.Enqueue(compositeActivity);
    while (compositeActivities.Count > 0)
    {
        CompositeActivity compositeActivity2 = (CompositeActivity)compositeActivities.Dequeue();
        childActivities = compositeActivity2.Activities;

        foreach (Activity activity in childActivities)
        {
            nestedActivities.Add(activity);
            if (activity is CompositeActivity)
                compositeActivities.Enqueue(activity);
        }
    }
    return (Activity[])nestedActivities.ToArray(typeof(Activity));
}
Protected Overrides Sub PerformLoad(ByVal serializationManager As IDesignerSerializationManager)
    Dim designerHost As IDesignerHost = CType(GetService(GetType(IDesignerHost)), IDesignerHost)
    Dim rootActivity As Activity = Nothing

    ' If a WorkflowType already exists, create an instance instead of reading from file
    If WorkflowType IsNot Nothing Then
        rootActivity = CType(Activator.CreateInstance(WorkflowType), Activity)
    Else
        ' Create a text reader out of the doc data
        Dim reader As TextReader = New StringReader(Me.Xoml)
        Try
            Using xmlReader As XmlReader = xmlReader.Create(reader)
                Dim xomlSerializer As New WorkflowMarkupSerializer()
                rootActivity = CType(xomlSerializer.Deserialize(xmlReader), Activity)
            End Using
        Finally
            reader.Close()
        End Try
    End If

    If rootActivity IsNot Nothing And designerHost IsNot Nothing Then
        AddObjectGraphToDesignerHost(designerHost, rootActivity)
    End If
End Sub

Private Shared Sub AddObjectGraphToDesignerHost(ByVal designerHost As IDesignerHost, ByVal activity As Activity)
    Dim Definitions_Class As New Guid("3FA84B23-B15B-4161-8EB8-37A54EFEEFC7")

    If designerHost IsNot Nothing Then
        Throw New ArgumentNullException("designerHost")
    End If
    If activity IsNot Nothing Then
        Throw New ArgumentNullException("activity")
    End If

    Dim rootSiteName As String = activity.QualifiedName
    If activity.Parent Is Nothing Then
        Dim fullClassName As String = CType(activity.UserData(Definitions_Class), String)
        If fullClassName Is Nothing Then
            fullClassName = activity.GetType().FullName
        End If
        rootSiteName = IIf(Not fullClassName.LastIndexOf(".") = -1, fullClassName.Substring(fullClassName.LastIndexOf(".") + 1), fullClassName)
        designerHost.Container.Add(activity, rootSiteName)
    Else
        designerHost.Container.Add(activity, activity.QualifiedName)
    End If

    If TypeOf activity Is CompositeActivity Then
        For Each activity2 As Activity In GetNestedActivities(CType(activity, CompositeActivity))
            designerHost.Container.Add(activity2, activity2.QualifiedName)
        Next
    End If
End Sub

Private Shared Function GetNestedActivities(ByVal compositeActivity As CompositeActivity) As Activity()
    If compositeActivity Is Nothing Then
        Throw New ArgumentNullException("compositeActivity")
    End If

    Dim childActivities As IList(Of Activity) = Nothing
    Dim nestedActivities As New ArrayList()
    Dim compositeActivities As New Queue()
    compositeActivities.Enqueue(compositeActivity)
    While compositeActivities.Count > 0
        Dim compositeActivity2 As CompositeActivity = CType(compositeActivities.Dequeue(), CompositeActivity)
        childActivities = compositeActivity2.Activities

        For Each activity As Activity In childActivities
            nestedActivities.Add(activity)
            If (activity Is compositeActivity) Then
                compositeActivities.Enqueue(activity)
            End If
        Next
    End While
    Return CType(nestedActivities.ToArray(GetType(Activity)), Activity())

End Function

Applies to