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:
private void OnSaveLayout()
{
XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(WorkspaceDockingManager);
layoutSerializer.Serialize(@".\StreamOUT.config");
}
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:
private void OnSaveLayout()
{
XmlTextWriter writer = new XmlTextWriter(@".\StreamOUT.config", new System.Text.UTF8Encoding());
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("StreamOUTConfig");
writer.WriteStartElement("MainWindowDockingManager");
XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(WorkspaceDockingManager);
layoutSerializer.Serialize(writer);
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
}
private void OnLoadLayout()
{
XmlTextReader reader = new XmlTextReader(@".\StreamOUT.Config");
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); reader.ReadEndElement();
}
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.