Table of Contents

Ribbon Serialization and Deserialization

End-users can use context menus to add commands to the Quick Access Toolbar and remove commands from it at runtime.

ribbon-qat-add-items-menu

ribbon-qat-remove-items-menu

The layout of commands in the Quick Access Toolbar can be saved to a stream, and loaded from it later (for instance, the next time your application runs). To do this, use the following layout serialization and deserialization methods:

  • RibbonControl.SaveLayout — Saves the layout of ribbon items in the Quick Access Toolbar to a stream.
  • RibbonControl.RestoreLayout — Reads the previously saved layout from a stream and applies it to the Quick Access Toolbar.
Note

All ribbon items must have unique names, which you can specify with the Name property (or alternatively, with the SerializationName property). Unique names ensure correct identification and serialization of ribbon items.

<mxb:ToolbarButtonItem Name="btnNew" .../>

The following example shows how you can save and restore the layout of ribbon items to/from a file.

string fileName = "ribbon_layout.xml";
private void BtnSave_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
    using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
    {
        ribbon.SaveLayout(stream);
    }
}

private void BtnLoad_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
    if (!File.Exists(fileName)) return;
    using (FileStream fileStream = File.OpenRead(fileName))
    {
        ribbon.RestoreLayout(fileStream);
    }
}