Serializing DocumentWindows with custom content

Docking/MDI for WPF Forum

Posted 8 years ago by John Dunn
Version: 10.2.0533
Avatar
I'd like to be able to save and restore the state of my DocHost. The content of each DocumentWindow is custom content which is loaded from another file. During the serialization I'm handling ObjectSerialized and am able to add my own tag data when the DocumentWindow is being serialized. In my case the tag is a GUID that I can look up in my database to load the window content.

That's all working fine - the issue is with the deserialization. I'm handling ObjectDeserialized which is getting fired but I'm not exactly sure what to do at this point. ObjectDeserialized gets called multiple times but when e.Node.Tag is my GUID e.Node is an XmlDocumentWindowRef and e.Item is a DockingTrack. At this point, armed with my GUID I can create my content for the DocumentWindow - what do I do with it? Do I need to create a DocumentWindow? Somehow turn in the XmlDocumentWindowRef for an actual DocumentWindow?

Comments (3)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi John,

The "refs" are only place holders for where a DocumentWindow or ToolWindow should be placed. The ObjectDeserialized/Serialized events should be firing for XmlDocumentWindow/Document and XmlToolWindow/ToolWindow, just like with the refs. That's where you'd want to save/load your custom data.


Actipro Software Support

Posted 8 years ago by John Dunn
Avatar
I must be missing something. Given the following code

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ActiproSoftware.Windows.Controls.Docking;
using ActiproSoftware.Windows.Controls.Docking.Serialization;

namespace Dock
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
      Loaded += Window1_Loaded;
      Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
    }

    void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      try
      {
        var ser = new DockSiteLayoutSerializer() { SerializationBehavior = DockSiteSerializationBehavior.All };
        ser.ObjectSerialized += (s, e2) =>
        {
          Console.WriteLine("Serializing {0}", e2.Item);
          if (e2.Item is DocumentWindow)
          {
            e2.Node.Tag = (e2.Item as DocumentWindow).Name;
          }
        };
        Console.WriteLine("Serializing Layout....");
        ser.SaveToFile("layout.xml", _DockSite);
        Console.WriteLine("Done");
      }
      catch (Exception)
      {
      }
    }

    void Window1_Loaded(object sender, RoutedEventArgs e)
    {
      try
      {
        var ser = new DockSiteLayoutSerializer() { SerializationBehavior = DockSiteSerializationBehavior.All };
        ser.ObjectDeserialized += (s, e2) =>
        {
          Console.WriteLine("Deserializing {0} {1}", e2.Item, e2.Node.Tag);
          string name = e2.Node.Tag as string;
          if (e2.Item is TabbedMdiContainer && !string.IsNullOrEmpty(name))
          {
            var window = new DocumentWindow(_DockSite)
            {
              Title = name,
              Name = name,
              Header = name,
              Content = new Canvas() { Background = Brushes.Chartreuse }
            };
          }
        };
        ser.LoadFromFile("layout.xml", _DockSite);
      }
      catch (Exception)
      {
      }
    }

    int index = 1;
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
      string name = string.Format("page{0}", index++);
      var window = new DocumentWindow(_DockSite)
      {
        Title = name,
        Name = name,
        Header = name,
        Content = new Canvas() { Background = Brushes.Chartreuse }
      };
      window.Activate();
    }
  }
}
I get the following when serializing after adding a couple of DocumentWindows

Serializing Layout....
Serializing DocumentWindow[Title=page1, UniqueId=e359c452-4e32-4020-9951-5bff724a58ae]
Serializing DocumentWindow[Title=page2, UniqueId=708274b1-57e3-418d-b5a6-7deb6be29807]
Serializing DocumentWindow[Title=page3, UniqueId=168aec32-5fee-43f2-8a40-11ff15646c00]
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing ActiproSoftware.Windows.Controls.Docking.Track
Serializing DocumentWindow[Title=page1, UniqueId=e359c452-4e32-4020-9951-5bff724a58ae]
Serializing DocumentWindow[Title=page2, UniqueId=708274b1-57e3-418d-b5a6-7deb6be29807]
Serializing DocumentWindow[Title=page3, UniqueId=168aec32-5fee-43f2-8a40-11ff15646c00]
Serializing ActiproSoftware.Windows.Controls.Docking.TabbedMdiContainer[Count=3, UniqueId=d4d0ce73-9f8e-4402-bda3-a8e093b799ee]
Serializing ActiproSoftware.Windows.Controls.Docking.TabbedMdiHost
Serializing ActiproSoftware.Windows.Controls.Docking.Workspace
Serializing ActiproSoftware.Windows.Controls.Docking.Primitives.AutoHideHost
Serializing ActiproSoftware.Windows.Controls.Docking.DockSite
and this when deserializing

Deserializing ActiproSoftware.Windows.Controls.Docking.Primitives.AutoHideHost 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track 
Deserializing ActiproSoftware.Windows.Controls.Docking.Track page1
Deserializing ActiproSoftware.Windows.Controls.Docking.Track page2
Deserializing ActiproSoftware.Windows.Controls.Docking.Track page3
Deserializing ActiproSoftware.Windows.Controls.Docking.TabbedMdiContainer[Count=0, UniqueId=7bec3fea-e5fe-4850-aefe-742c8dab479f] 
Deserializing ActiproSoftware.Windows.Controls.Docking.TabbedMdiHost 
Deserializing ActiproSoftware.Windows.Controls.Docking.DockSite 
The only place I see my tag while deserializing is during the Docking.Track deserialization.
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi John,

If your documents do not exist in the DockSite, or are destroyed on close, then you'd need to add a handler for the DockingWindowDeserializing event and create them there. You can set DocumentWindowDeserializationBehavior to AutoCreate on the DockSiteLayoutSerializer, to have the instance created for you, in which case you'd just need to initialize it.

Because it can't find the DocumentWindows and one isn't created (either through AutoCreate or a DockingWindowDeserializing handler), it creates Tracks for them. This allows you to create the DocumentWindows later, and have them open in their previous location.


Actipro Software Support

The latest build of this product (v2018.1 build 0675) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.