Search
XmlSerializationContext.Completed Event
See Also
 






Raised when the serialization or deserialization has completed successfully.

Namespace: MindFusion.Scheduling
Assembly: MindFusion.Scheduling

 Syntax

C#  Copy Code

public event EventHandler Completed

Visual Basic  Copy Code

Public Event Completed As EventHandler

 Event Data

Completed event handlers receive an argument of type EventArgs.

 Remarks

You can use this event in your custom items to deserialize references to other Item and/or Resource objects.

 Example

The following example demonstrates a technique for loading references to Item objects when deserializing a custom item.

C#  Copy Code

/// <summary>
/// Custom appointment class.
/// </summary>
public class MyItem : Appointment
{
    /// <summary>
    /// Initializes a new instance of the MyItem class.
    /// </summary>
    public MyItem()
    {
        referencedItems = new List<Item>();
        itemIds = new List<string>();
    }

    /// <summary>
    /// Appointment.SaveTo override.
    /// </summary>
    public override void SaveTo(BinaryWriter writer, BinarySerializationContext context)
    {
        base.SaveTo(writer, context);

        // Write the id's of the referenced items
        context.WriteInt(referencedItems.Count, writer);
        foreach (Item item in referencedItems)
            context.WriteString(item.Id, writer);
    }

    /// <summary>
    /// Appointment.LoadFrom override.
    /// </summary>
    public override void LoadFrom(BinaryReader reader, BinarySerializationContext context)
    {
        base.LoadFrom(reader, context);

        // Read the id's of the referenced items. We will obtain the actual references
        // after the deserialization has been completed, because the items might not
        // be available at this time
        int count = context.ReadInt(reader);
        itemIds = new List<string>();
        for (int i = 0; i < count; i++)
            itemIds.Add(context.ReadString(reader));

        // Register an event handler
        context.Completed += new EventHandler(OnLoadCompleted);
    }

    /// <summary>
    /// Handles the Completed event on the SerializationContext object.
    /// </summary>
    private void OnLoadCompleted(object sender, EventArgs e)
    {
        SerializationContext context = sender as SerializationContext;

        referencedItems.Clear();
        foreach (string id in itemIds)
        {
            if (context.Schedule.Items.Contains(id))
                referencedItems.Add(context.Schedule.Items[id]);
        }

        itemIds.Clear();

        // Unregister the event handler
        context.Completed -= new EventHandler(OnLoadCompleted);
    }


    /// <summary>
    /// A list with the item references.
    /// </summary>
    private List<Item> referencedItems;

    /// <summary>
    /// A temporary list holding the IDs of the referenced items during deserialization.
    /// </summary>
    private List<string> itemIds;
}

Visual Basic  Copy Code

''' <summary>
''' Custom appointment class.
''' </summary>
Public Class MyItem
    Inherits Appointment

    ''' <summary>
    ''' Initializes a new instance of the MyItem class.
    ''' </summary>
    Public Sub New()

        referencedItems = New List(Of Item)()
        itemIds = New List(Of String)()

    End Sub

    ''' <summary>
    ''' Appointment.SaveTo override.
    ''' </summary>
    Public Overrides Sub SaveTo(ByVal writer As BinaryWriter, ByVal context As BinarySerializationContext)

        MyBase.SaveTo(writer, context)

        ' Write the id's of the referenced items
        context.WriteInt(referencedItems.Count, writer)
        For Each item As Item in referencedItems
            context.WriteString(item.Id, writer)
        Next item

    End Sub

    ''' <summary>
    ''' Appointment.LoadFrom override.
    ''' </summary>
    Public Overrides Sub LoadFrom(ByVal reader As BinaryReader, ByVal context As BinarySerializationContext)

        MyBase.LoadFrom(reader, context)

        ' Read the id's of the referenced items. We will obtain the actual references
        ' after the deserialization has been completed, because the items might not
        ' be available at this time
        Dim count As Integer = context.ReadInt(reader)
        itemIds = new List(Of String)()
        For i As Integer = 0 To count
            itemIds.Add(context.ReadString(reader))
        Next i

        ' Register an event handler
        AddHandler context.Completed, New EventHandler(AddressOf Me.OnLoadCompleted)

    End Sub

    ' <summary>
    ' Handles the Completed event on the SerializationContext object.
    ' </summary>
    Private Sub OnLoadCompleted(ByVal sender As Object, ByVal e As EventArgs)

        Dim context As SerializationContext = CType(sender, SerializationContext)

        referencedItems.Clear()
        For Each id As String In itemIds

            If context.Schedule.Items.Contains(id) Then
                referencedItems.Add(context.Schedule.Items(id))
            End If

        Next id

        itemIds.Clear()

        ' Unregister the event handler
        RemoveHandler context.Completed, New EventHandler(AddressOf Me.OnLoadCompleted)

    End Sub


    ''' <summary>
    ''' A list with the item references.
    ''' </summary>
    Private referencedItems As List(Of Item)

    ''' <summary>
    ''' A temporary list holding the IDs of the referenced items during deserialization.
    ''' </summary>
    Private itemIds As List(Of String)

End Class

 See Also