[Version 2.0] How to restore layout?

Mar 23, 2012 at 4:08 PM
Edited Mar 23, 2012 at 4:10 PM

Hi,

I have some panels and a method SaveLayout which is called on main window Closed event. This method's logic is copy-pasted from AvalonDock.TestApp. I wanted to restore layout when Loaded event occurs for main window but I get an exception: "Unable to set the DocumentsSource property if LayoutDocument objects are already present in the model". LoadLayout logic is copy-pasted too)) Xaml code can be viewed here http://avalondock.codeplex.com/discussions/349636. Load and saveLayout:

 

        private const string LAYOUT_CONFIG = "Panels.config";
        private static void LoadLayout(DockingManager dockingManager)
        {
            var serializer = new XmlLayoutSerializer(dockingManager);
            serializer.LayoutSerializationCallback += (s, args) =>
            {
                //if (args.Model.ContentId == "m_PropertyGridHost")
                //    args.Content = m_PropertyGridHost;
            };

            if (!File.Exists(LAYOUT_CONFIG))
                return;

            try
            {
                using (var stream = new StreamReader(LAYOUT_CONFIG))
                    serializer.Deserialize(stream);
            }
            catch (Exception)
            {
                Debug.Assert(false);
            }
        }
        private static void SaveLayout(DockingManager dockingManager)
        {
            var serializer = new XmlLayoutSerializer(dockingManager);
            using (var stream = new StreamWriter(LAYOUT_CONFIG))
                serializer.Serialize(stream);
        }

 

Can anyone help?

Mar 23, 2012 at 4:49 PM

Layout serialization is not yet supported when using the DocumentsSource/AnchorablesSource property (MVVM scenario).
I'm working on this and I hope to release this feature by the end of next week.

Thanks

Ado

Mar 23, 2012 at 4:56 PM

Thanks Ado. I will wait for that release

Apr 19, 2012 at 4:45 PM

Hi Ado.

Did you do something on this issue? I tried to find a simple solution which would satisfy me. I have added two lines of code into the DockingManager.cs in function AttachAnchorablesSource:

 

            if (anchorablePane != null)
            {
                foreach (var anchorableToImport in anchorablesSource)
                {
                    var contentId = anchorableToImport.GetType().ToString();
                    var newModel = new LayoutAnchorable { Content = anchorableToImport, ContentId = contentId };
                    anchorablePane.Children.Add(newModel);
                }
            }

This allowed me to load anchorables content through the LayoutSerializationCallback switching between class names. See if it is usefull.

Thanks for your work! It's really great.

 

Apr 19, 2012 at 6:56 PM

Yes, it can works but what happen if someone has the two or more anchorable wof the same type?

May 3, 2012 at 5:09 PM
adospace wrote:

I'm working on this and I hope to release this feature by the end of next week.

Any updates?

Jan 22, 2013 at 1:41 PM

Has this feature been implemented yet?  If so, is there a working example someone can post?  Thanks.

Jan 23, 2013 at 8:03 AM

I think this feature is implemented, because I load my layouts in a very similar way and don't have any problems.

The only difference there may be is that I clear the current list of my tool viewmodels, before I deserialize a new layout.