State Persistence in WPF Tabbed MDI Form

18 Nov 20183 minutes to read

This topic illustrates how to load and save the dock state in various places. It also gives information about resetting and deleting the states.

Load and Save in Isolated Storage

The simplest way to load and save the WPF Tabbed MDI Form state is by using its built-in Isolated Storage methods. To load, save, and reset the state, use the following code.

// Save the state to Isolated Storage
DocContainer.SaveDockState();

// Load the state from Isolated Storage
DocContainer.LoadDockState();

// Reset and delete the Isolated Storage used by the DocumentContainer
DocContainer.DeleteInternalIsolatedStorage();

Load and Save in a Stream

You can also save the WPF Tabbed MDI Form state to any Stream using a BinaryFormatter (or any other IFormatter). The SaveDockState(IFormatter) and LoadDockState(IFormatter) overloads accept a formatter that the WPF Tabbed MDI Form uses to write/read its state.

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

BinaryFormatter formatter = new BinaryFormatter();

using (FileStream stream = new FileStream("dockstate.bin", FileMode.Create))
{
    DocContainer.SaveDockState(formatter, stream);
}

using (FileStream stream = new FileStream("dockstate.bin", FileMode.Open))
{
    DocContainer.LoadDockState(formatter, stream);
}

Load and Save in XML

You can save and load the WPF Tabbed MDI Form state in XML format using either the BinaryFormatter or the SoapFormatter. The StorageFormat parameter selects between XML and binary storage.

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

string xmlPath = @"d:\docum_xml.xml";

// Save in XML using BinaryFormatter
BinaryFormatter binaryFormatter = new BinaryFormatter();
DocContainer.SaveDockState(binaryFormatter, StorageFormat.Xml, xmlPath);

// Save in XML using SoapFormatter
SoapFormatter soapFormatter = new SoapFormatter();
DocContainer.SaveDockState(soapFormatter, StorageFormat.Xml, xmlPath);

// Load XML using BinaryFormatter
DocContainer.LoadDockState(binaryFormatter, StorageFormat.Xml, xmlPath);

// Load XML using SoapFormatter
DocContainer.LoadDockState(soapFormatter, StorageFormat.Xml, xmlPath);

Load and Save in Binary

You can also save and load the WPF Tabbed MDI Form state in a binary file using either BinaryFormatter or SoapFormatter.

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

string binPath = @"d:\docum_bin.bin";

// Save using BinaryFormatter
BinaryFormatter binaryFormatter = new BinaryFormatter();
DocContainer.SaveDockState(binaryFormatter, StorageFormat.Binary, binPath);

// Save using SoapFormatter
SoapFormatter soapFormatter = new SoapFormatter();
DocContainer.SaveDockState(soapFormatter, StorageFormat.Binary, binPath);

// Load using BinaryFormatter
DocContainer.LoadDockState(binaryFormatter, StorageFormat.Binary, binPath);

// Load using SoapFormatter
DocContainer.LoadDockState(soapFormatter, StorageFormat.Binary, binPath);

Reset the State

You can reset the WPF Tabbed MDI Form state using the ResetState method.

// Reset the state
DocContainer.ResetState();

Delete the State

You can delete a saved state file or the Isolated Storage state using the DeleteDockState method overloads.

// Delete a state file from disk
DocContainer.DeleteDockState(@"d:\docum_xml.xml");
DocContainer.DeleteDockState(@"d:\docum_bin.bin");

// Delete the Isolated Storage state
DocContainer.DeleteDockState();