AvalonDock icon indicating copy to clipboard operation
AvalonDock copied to clipboard

Saving window layouts

Open RezanYldz opened this issue 2 years ago • 5 comments

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.

RezanYldz avatar May 13 '22 14:05 RezanYldz

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.

BrentFarris avatar May 13 '22 17:05 BrentFarris

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

Skaptor avatar May 13 '22 18:05 Skaptor

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?

RezanYldz avatar May 18 '22 09:05 RezanYldz

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.

RezanYldz avatar Oct 06 '22 13:10 RezanYldz

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`

mbowles201 avatar Dec 28 '22 20:12 mbowles201