Click here to Skip to main content
15,886,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have been trying to load and save the layout of an Avalon docking manager with mixed success.

Just doing the basic thing works as expected:

C#
private void OnSaveLayout()
{
    XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(WorkspaceDockingManager);
    layoutSerializer.Serialize(@".\StreamOUT.config");
}

C#
private void OnLoadLayout()
{
    XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(WorkspaceDockingManager);
    layoutSerializer.LayoutSerializationCallback += (s, e) =>
    {
        WorkspaceViewModel workspace =
            (from ws in Workspaces
             where e.Model.ContentId == ws.ContentId
             select ws).First();
        if (workspace != null)
        {
            e.Content = workspace;
        }
    };
    layoutSerializer.Deserialize(@".\StreamOUT.config");
}


But when I try to serialise to and from an XmlTextWriter and XmlTextReader, respectively, I get a null reference exception from deep inside the AvalonDock code. From line 227 in ReadXml of Xceed.Wpf.AvalonDock.Layout.LayoutGroup.cs to be precise. This is the line where it tries to deserialize after matching against several "Layout*" elements. Obviously, the element the reader is on at this time is not one of these expected so the serialiser object is still null when it tries to reference it. Currently, I don't have the source of AvalonDock in my solution so I can't debug and see exactly where the reader is nor can I single-step through the deserialisation process.

I don't understand what I do wrong and I failed to locate any answers. Here is what I have:

C#
private void OnSaveLayout()
{
    ///TODO: All this serialisation code should probably be in a
    ///      proper serialiser for the MainWindowViewModel and only
    ///      called from the save command.

    XmlTextWriter writer = new XmlTextWriter(@".\StreamOUT.config", new System.Text.UTF8Encoding());
    writer.Formatting = Formatting.Indented;
    writer.WriteStartElement("StreamOUTConfig");

    ///TODO: Serialize AudioChain

    ///TODO: Serialise Workspaces

    ///TODO: Serialise all other properties

    /// Serialize main window avalon docking manager:
    writer.WriteStartElement("MainWindowDockingManager");
    XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(WorkspaceDockingManager);
    layoutSerializer.Serialize(writer);
    writer.WriteEndElement();

    ///TODO: Serialise avalon docking manager for each workspace:

    writer.WriteEndElement();
    writer.Close();
}


C#
private void OnLoadLayout()
{
    XmlTextReader reader = new XmlTextReader(@".\StreamOUT.Config");

    ///TODO: All this deserialisation code should probably be in a
    ///      proper deserialiser for the MainWindowViewModel and only
    ///      called from the load command.

    ///TODO: Deserialize AudioChain

    ///TODO: Deserialise Workspaces

    ///TODO: Deserialise all other properties

    /// Deserialize main window avalon docking manager:
    reader.ReadToDescendant("MainWindowDockingManager");
    reader.ReadToDescendant("LayoutRoot");
    XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(WorkspaceDockingManager);
    layoutSerializer.LayoutSerializationCallback += (s, e) =>
    {
        WorkspaceViewModel workspace =
            (from ws in Workspaces
             where e.Model.ContentId == ws.ContentId
             select ws).First();
        if (workspace != null)
        {
            e.Content = workspace;
        }
    };
    layoutSerializer.Deserialize(reader); ///TODO: null reference :(
    reader.ReadEndElement();

    ///TODO: Deserialise avalon docking manager for each workspace:
}


Of course the other classes, properties and general stuff referenced in these snippets are defined and hopefully work as expected.

I hope that I just made a stupid, silly mistake and that you can point me in the right direction quickly.

Thanks in advance.

P.S. This question may or may not have the same or a similar answer as this.
Posted
Updated 21-Dec-13 7:30am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900