Serialization Support in Windows Forms Ribbon (RibbonControlAdv)

29 Apr 20217 minutes to read

The RibbonControlAdv has built-in serialization support to serialize the entire Ribbon control state and details of the layout mode. It also provides supports to save and load the Ribbon at any time while running the application, either with simplified or normal layout.

States which get serialized

  • The LayoutMode and EnableSimplifiedLayoutMode properties of RibbonControlAdv can be persisted.
  • The items in the QuickAccessToolBar that are added through the “Customize Quick Access ToolBar” can be persisted.
  • Items added through the context menu that appears while clicking the dropdown arrow to the right of the QuickAccessToolBar can be persisted.
  • The collapsed / expanded / floating state of the RibbonPanel can be persisted.
  • The position of the QuickAccessToolBar, either below or above the ribbon panel can be persisted.

Save state

Save Ribbon state

Through Isolated storage

The Ribbon state can be saved in the isolated storage using the SaveState function in the Ribbon. The isolated storage can be located in the following location: “C:\Users<user>\AppData\Local\IsolatedStorage”.

//Saves the current state in a Isolated storage
this.ribbonControlAdv1.SaveState();
Me.ribbonControlAdv1.SaveState()

Through XML

The Ribbon state can be saved in an xml file and stored in a specific location using the AppStateSerializer. The instance of AppStateSerializer must be passed as an argument to the SaveState function in the Ribbon.

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/RibbonState");
this.wordribbon.SaveState(serializer);
serializer.PersistNow();
Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/RibbonState")
Me.wordribbon.SaveState(serializer)
serializer.PersistNow()

Save QAT state

Ribbon control provides an option to save the state of QAT alone instead of saving all Ribbon states using the SerializationOptions. The SerializationOptions consists of two boolean properties such as SerializeQATItems and SerializeTabItems which are set as “True” by default. To serialize only the QAT state, the SerializeTabItems property must be set as “False”. This instance of SerializationOptions must be passed as an argument to SaveState function in addition to the AppStateSerializer instance.

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/QATState");
RibbonControlAdv.SerializationOptions options = new RibbonControlAdv.SerializationOptions();
options.SerializeTabItems = false;
this.wordribbon.SaveState(serializer);
serializer.PersistNow();
Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/QATState")
Dim options As RibbonControlAdv.SerializationOptions = New RibbonControlAdv.SerializationOptions()
options.SerializeTabItems = False
Me.wordribbon.SaveState(serializer)
serializer.PersistNow()

Save Tab state

Ribbon control provides an option to save the state of Ribbon Tabs alone instead of saving all Ribbon states using the SerializationOptions. The SerializationOptions consists of two boolean properties such as SerializeQATItems and SerializeTabItems which are set as “True” by default. To serialize only the Tab state, the SerializeQATItems property must be set as “False”. This instance of SerializationOptions must be passed as argument to the SaveState in addition to the AppStateSerializer instance.

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/TabState");
RibbonControlAdv.SerializationOptions options = new RibbonControlAdv.SerializationOptions();
options.SerializeQATItems = false;
this.wordribbon.SaveState(serializer);
serializer.PersistNow();
Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/TabState")
Dim options As RibbonControlAdv.SerializationOptions = New RibbonControlAdv.SerializationOptions()
options.SerializeQATItems = False
Me.wordribbon.SaveState(serializer)
serializer.PersistNow()

Load State

Load Ribbon state

Through Isolated storage

To load the saved ribbon state from the isolated storage, use the below code

//Loads the serialized state from the Isolated storage
this.ribbonControlAdv1.LoadState();
Me.ribbonControlAdv1.LoadState()

Through XML

The saved Ribbon state can be loaded from an xml file using the AppStateSerializer. The instance of AppStateSerializer must be passed as an argument to the LoadState function in the Ribbon.

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/RibbonState");
this.wordribbon.LoadState(serializer);
Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/RibbonState")
Me.wordribbon.LoadState(serializer)

Load QAT state

Ribbon control provides an option to load the state of QAT alone instead of loading all Ribbon states using the DeserializationOptions. The DeserializationOptions consists of two boolean properties such as DeserializeQATItems and DeserializeTabItems which are set as “True” by default. To deserialize only the QAT state, the DeserializeTabItems property must be set as “False”. The instance of DeserializationOptions must be passed as argument to the LoadState function in addition to the AppStateSerializer instance.

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/QATState");
RibbonControlAdv.DeserializationOptions options = new RibbonControlAdv.DeserializationOptions();
options.DeserializeTabItems = false;
this.wordribbon.LoadState(serializer);
Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/QATState")
Dim options As RibbonControlAdv.DeserializationOptions = New RibbonControlAdv.DeserializationOptions()
options.DeserializeTabItems = False
Me.wordribbon.LoadState(serializer)

Load Tabs state

Ribbon control provides an option to load only the state of Ribbon Tabs instead of all Ribbon states using the DeserializationOptions. The DeserializationOptions consists of two boolean properties such as DeserializeQATItems and DeserializeTabItems which are set as “True” by default. To deserialize only the Tab state, the DeserializeQATItems property must be set as “False”. This instance of DeserializationOptions must then be passed as argument to the LoadState function in addition to the AppStateSerializer instance.

AppStateSerializer serializer = new AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/TabState");
RibbonControlAdv.DeserializationOptions options = new RibbonControlAdv.DeserializationOptions();
options.DeserializeQATItems = false;
this.wordribbon.LoadState(serializer);
Dim serializer As AppStateSerializer = New AppStateSerializer(SerializeMode.XMLFile, "D:/Ribbon Serialization/TabState")
Dim options As RibbonControlAdv.DeserializationOptions = New RibbonControlAdv.DeserializationOptions()
options.DeserializeQATItems = False
Me.wordribbon.LoadState(serializer)