AvalonDock
AvalonDock copied to clipboard
Saving window layouts
Hello everyone, How can I save these settings and restore them on subsequent launches when the user drags and pins the windows, closes or collapses them?
Best regards.
I haven't used this library yet, just so happen to be looking at it today for possibly using it. On the readme page of this repo (tutorial part 5) I saw a link to save/load layouts, might be what you're looking for.
It currently does not work. You will have to manually hook the window OnClosing event with something like this:
public string GetDockingManagerLayout()
{
if (DockingManager is null)
{
throw new InvalidOperationException("Cannot get docking manager layout as it is null");
}
using StringWriter writer = new();
XmlLayoutSerializer xmlLayout = new(DockingManager);
xmlLayout.Serialize(writer);
return writer.ToString();
}
protected override void OnClosing(CancelEventArgs e)
{
try
{
base.OnClosing(e);
}
catch (Exception ex)
{
Log.Error(ex, "Something broke");
}
}
`
Where `DockingManager` is your DockingManager
First of all, thank you for the help.
After some effort, I applied the tutorial to my own project.
I have a LayoutDocumentPane that I don't want it to save. How can I do that?
Hello, I'm having a problem implementing this, when I close and save a layoutpane and reopen the program and try to add the layoutpane, it doesn't add it.
Not sure if this will work for Dirkster99's version, but 4 yrs ago I did this to save and load layouts. Its is in the ViewModel with Caliburn.Micro via Message.Attach "[Event Loaded] = [Action LoadLayout($source)]; [Event Unloaded] = [Action SaveLayout($source)];" on the DockingManager
` #region load and save layout
/// <summary>
/// Load the layout from XML
/// </summary>
/// <param name="aDock">The framework element DockingManager, passed in from a Caliburn Message</param>
public void LoadLayout(object aDock)
{
DockingManager frameworkElement = aDock as DockingManager;
var loadFromXml = false;
PrimaryDock = frameworkElement;
if (File.Exists(LayoutFilePath))
{
//Restore the layout
var serializer = new XmlLayoutSerializer(frameworkElement);
using (var stream = new StreamReader(LayoutFilePath))
serializer.Deserialize(stream);
}
}
/// <summary>
/// Save the layout to XML
/// </summary>
/// <param name="aDock">The framework element DockingManager, passed in from a Caliburn Message</param>
public void SaveLayout(object aDock)
{
DockingManager frameworkElement = aDock as DockingManager;
var loadFromXml = false;
var serializer = new XmlLayoutSerializer(frameworkElement);
using (var stream = new StreamWriter(LayoutFilePath))
serializer.Serialize(stream);
}
#endregion`