MindFusion.Wpf Pack Programmer's Guide
Schedule.RegisterItemClass Method (Type, String, Int32)
See Also
 





Registers a schedule item class for serialization support.

Namespace: MindFusion.Scheduling
Assembly: MindFusion.Scheduling.Wpf

 Syntax

C#  Copy Code

public static void RegisterItemClass (
    Type itemClass,
    string classId,
    int classVersion
)

Visual Basic  Copy Code

Public Shared Sub RegisterItemClass( _
    itemClass As Type, _
    classId As String, _
    classVersion As Integer _
)

 Parameters

itemClass
A .NET Type instance identifying the item class.
classId

A class identifier to use when saving and loading items of the specified type.

classVersion

A revision number of the item's class serialization format.

 Remarks

When using custom item classes, use this method to enable saving and loading the custom item objects to/from XML or binary files. The class id string is written before each item's data, and when loading items later that string indicates to the schedule what class instance to create and let it load the subsequent data. The following class identifiers are reserved and should not be used: 'std:appointment', 'std:resource', 'std:contact', 'std:location' and 'std:task'.

The version identifier must be increased when the format in which items are saved changes. It is used when loading files to let your application load items data from an older format.

 Note

All custom item classes must provide a parameterless constructor. Failing to do so would result in exceptions during the load process, since the schedule will be unable to create custom items.

 Example

The following example demonstrates how to use custom item classes with the schedule and how to load and save a schedule containing custom items. The example presumes that schedule already references an existing Schedule object.

Running the example should dump the following line to the output console:

Hello, friend!

C#  Copy Code

/// <summary>
/// The declaration of the custom item class.
/// </summary>
class CustomItem : Appointment
{
    /// <summary>
    /// The parameterless constructor, required by the Schedule.
    /// </summary>
    public CustomItem()
    {
        _customField = "This is my field.";
    }

    public override void LoadFrom(
        System.Xml.XmlReader reader,
        SerializationContext context)
    {
        // Don't forget to invoke LoadFrom on the base class
        base.LoadFrom(reader, context);

        _customField = reader.ReadElementString("customField");
    }

    public override void SaveTo(
        System.Xml.XmlWriter writer,
        SerializationContext context)
    {
        base.SaveTo(writer, context);

        writer.WriteElementString("customField", _customField);
    }


    public string CustomField
    {
        get { return _customField; }
        set { _customField = value; }
    }


    private string _customField;
}

// ...

// Register our custom item class for use with the schedule
Schedule.RegisterItemClass(typeof(CustomItem), "customItemId", 1);

// Create a new custom item and add it to the schedule
CustomItem customItem = new CustomItem();
customItem.CustomField = "Hello, friend!";
schedule.Items.Add(customItem);

// Save the schedule to a file.
// Make sure the specified path exists on your drive
schedule.SaveTo(@"c:\schedule.save");

// Clear the schedule
schedule.Items.Clear();

// Load the previously saved schedule data
schedule.LoadFrom(@"c:\schedule.save");

// Test the data
foreach (Item item in schedule.Items)
{
    if (item is CustomItem)
    {
        System.Diagnostics.Debug.WriteLine(
            (item as CustomItem).CustomField);
    }
}

Visual Basic  Copy Code

' <summary>
' The declaration of the custom item class.
' </summary>
Class CustomItem
    Inherits Appointment

    ' <summary>
    ' The parameterless constructor, required by the Schedule.
    ' </summary>
    Public Sub New()

        _customField = "This is my field."

    End Sub

    Public Overloads Overrides Sub LoadFrom( _
        ByVal reader As System.Xml.XmlReader, _
        ByVal context As SerializationContext)

        ' Don't forget to invoke LoadFrom on the base class
        MyBase.LoadFrom(reader, context)

        _customField = reader.ReadElementString("customField")

    End Sub

    Public Overloads Overrides Sub SaveTo( _
        ByVal writer As System.Xml.XmlWriter, _
        ByVal context As SerializationContext)

        MyBase.SaveTo(writer, context)

        writer.WriteElementString("customField", _customField)

    End Sub


    Public Property CustomField() As String

        Get
            Return _customField
        End Get

        Set(ByVal Value As String)
            _customField = Value
        End Set

    End Property


    Private _customField As String

End Class

' ...

' Register our custom item class for use with the schedule
schedule.RegisterItemClass(GetType(CustomItem), "customItemId", 1)

' Create a new custom item and add it to the schedule
Dim customItem As New CustomItem
customItem.CustomField = "Hello, friend!"
schedule.Items.Add(customItem)

' Save the schedule to a file.
' Make sure the specified path exists on your drive
schedule.SaveTo("c:\schedule.save")

' Clear the schedule
schedule.Items.Clear()

' Load the previously saved schedule data
schedule.LoadFrom("c:\schedule.save")

' Test the data
Dim item As Item
For Each item In schedule.Items

    If TypeOf item Is CustomItem Then

        System.Diagnostics.Debug.WriteLine( _
            CType(item, CustomItem).CustomField)

    End If

Next item

 See Also

RegisterItemClass Method Overload List
Schedule Members
Schedule Class
MindFusion.Scheduling Namespace