Serialization Support in Windows Forms TabbedMDI

29 Apr 20214 minutes to read

The AppStateSerializer class is a serialization utility that allows multiple components in an application to access a common disk I/O medium for state persistence. Using the same storage medium for persisting the state information across components, without overlying them together, helps avoid the file clutter that is bound to occur by components using distinct files.

The TabGroupStates can be serialized in,

  • Binary Format
  • XML Format
  • Isolated Storage
  • Memory Stream
  • Default Storage Medium (null parameter constructor).

The TabbedMDIManager class uses the SaveTabGroupState and LoadTabGroupState methods to save and load TabGroupState respectively.

Methods table

Methods Description
SaveTabGroupStates Saves the current tab group's state information to the specified persistence medium.
LoadTabGroupStates Reads the tab group's information from the specified persistent store and applies the new state.
ClearSavedTabGroupState Clears the state of the saved tab group.

Make sure to call PersistNow method when you are done with writing into the serializer.

NOTE

The LoadTabGroupStates and SaveTabGroupStates methods get called automatically when you enable/disable TabbedMDI.

// To Save

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "MyFile");

this.tabbedMdiManager.SaveTabGroupStates(serializer);

serializer.PersistNow();



// To Load

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "MyFile");

this.tabbedMdiManager.LoadTabGroupStates(serializer);
' To Save

Dim serializer As New AppStateSerializer(SerializeMode.XMLFile, "MyFile")

Me.tabbedMdiManager.SaveTabGroupStates(serializer)

serializer.PersistNow()



' To Load

Dim serializer As New AppStateSerializer(SerializeMode.XMLFile, "MyFile")

Me.tabbedMdiManager.LoadTabGroupStates(serializer)

Singleton method

The AppStateSerializer is set to use the IsolatedStorage format by default. When the user does not need the states to store in the Isolated Storage area, it could be changed by initializing the AppStateSerializer’s Singleton at the beginning of an application.

//Initializing the AppStateSerializer's Singleton at the beginning of an application.

AppStateSerializer.InitializeSingleton(SerializeMode.XMLFile, "MyFile");
' Initializing the AppStateSerializer's Singleton at the beginning of an application.

AppStateSerializer.InitializeSingleton(SerializeMode.XMLFile, "MyFile")

Below are the code snippets for different storage mediums.

To serialize in Binary Format, use the below code.

// To Save

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.BinaryFile, "MyFile");

this.tabbedMdiManager.SaveTabGroupStates(serializer);

serializer.PersistNow();



// To Load

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.BinaryFile, "MyFile");

this.tabbedMdiManager.LoadTabGroupStates(serializer);
' To Save

Dim serializer As New AppStateSerializer(SerializeMode.BinaryFile, "MyFile")

Me.tabbedMdiManager.SaveTabGroupStates(serializer)

serializer.PersistNow()



' To Load

Dim serializer As New AppStateSerializer(SerializeMode.BinaryFile, "MyFile")

Me.tabbedMdiManager.LoadTabGroupStates(serializer)

To serialize in Isolated Storage, use the below code.

// To Save

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.IsolatedStorage, "MyFile");

this.tabbedMdiManager.SaveTabGroupStates(serializer);

serializer.PersistNow();



// To Load

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.IsolatedStorage, "MyFile");

this.tabbedMdiManager.LoadTabGroupStates(serializer);
' To Save

Dim serializer As New AppStateSerializer(SerializeMode.IsolatedStorage, "MyFile")

Me.tabbedMdiManager.SaveTabGroupStates(serializer)

serializer.PersistNow()



' To Load

Dim serializer As New AppStateSerializer(SerializeMode.IsolatedStorage, "MyFile")

Me.tabbedMdiManager.LoadTabGroupStates(serializer)

To serialize in Memory Stream, use the below code.

// To Save

System.IO.MemoryStream ms = new MemoryStream();



AppStateSerializer serializer = new AppStateSerializer(SerializeMode.BinaryFmtStream, ms);

this.tabbedMDIManager.SaveTabGroupStates(serializer);

serializer.PersistNow();



// To Load

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.IsolatedStorage, ms);

this.tabbedMdiManager.LoadTabGroupStates(serializer);
' To Save

Dim ms As MemoryStream = New MemoryStream()



Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.BinaryFmtStream, ms)

Me.tabbedMdiManager.SaveDockState(serializer)

serializer.PersistNow()



' To Load

Dim serializer As New AppStateSerializer(SerializeMode.BinaryFmtStream, ms)

Me.tabbedMdiManager.LoadTabGroupStates(serializer)