Restoring Document Windows in Docking

Docking/MDI for WPF Forum

Posted 2 years ago by Mark Mazurik
Version: 22.1.0
Avatar

I am using your Docking Sample code that came with the download.

I have added a couple of menu items to save and restore layout:

private void OnSaveLayout(object sender, RoutedEventArgs e) {
   string layout = new DockSiteLayoutSerializer().SaveToString(dockSite);
   File.WriteAllText("C:\\Temp\\Layout.txt", layout);
}

private void OnRestoreLayout(object sender, RoutedEventArgs e) {
   string layout = File.ReadAllText("C:\\Temp\\Layout.txt");
   new DockSiteLayoutSerializer().LoadFromString(layout, dockSite);
}

I also modified the section that creates new documents as follows:

    // Create the document
   var documentWindow = new EditorDocumentWindow(data, text) {
      SerializationId = serializationId.ToString()
   };
   ++serializationId;
   dockSite.DocumentWindows.Add(documentWindow);

   // Activate the document
   documentWindow.Activate();
}

private int serializationId = 1;

Using the UI I have moved the Solution Explorer to the top and split the document area into two sections having two documents in it each. I then use the Save Layout and close the application. Then I restart the modified sample and click the Restore Layout. The Solution Explorer is moved to the top, but the document section is a single section with a single document. If I manually add the documents back then and use Restore Layout again, the documents remain in the same section instead of being split into two document areas.

My ultimate goal is to restore the document area with all document windows that were there when the layout was saved. I would appreciate any assistance that you could offer in this area. As I am using the sample application you can suggest changes to that.

Best Regards

Mark Mazurik

Comments (2)

Posted 2 years ago by Mark Mazurik
Avatar

OK. I see that if I do something like:

private void OnSaveLayout(object sender, RoutedEventArgs e) {
   DockSiteLayoutSerializer serializer = new DockSiteLayoutSerializer() {
      SerializationBehavior =  DockSiteSerializationBehavior.All
   };
   string layout = serializer.SaveToString(dockSite);
   File.WriteAllText("C:\\Temp\\Layout.txt", layout);
}

private void OnRestoreLayout(object sender, RoutedEventArgs e) {
   string layout = File.ReadAllText("C:\\Temp\\Layout.txt");
   DockSiteLayoutSerializer serializer = new DockSiteLayoutSerializer() {
      SerializationBehavior = DockSiteSerializationBehavior.All,
      DocumentWindowDeserializationBehavior = DockingWindowDeserializationBehavior.AutoCreate
   };
   serializer.LoadFromString(layout, dockSite);
}

AND ensure that the document windows exist that they are restored correctly.  However, if I do not create the document windows by using File/New three times, they are still created in the layout but the tabs have no names (save for the originally loaded About.txt) and they do not have content.

Then I moved on to

private void OnRestoreLayout(object sender, RoutedEventArgs e) {
   string layout = File.ReadAllText("C:\\Temp\\Layout.txt");
   DockSiteLayoutSerializer serializer = new DockSiteLayoutSerializer() {
      SerializationBehavior = DockSiteSerializationBehavior.All
   };
   serializer.DockingWindowDeserializing += (o, args) => {
      if (args.Window == null) {
         var data = new DocumentData();
         data.FileName = String.Format("Document{0}{1}", ++documentIndex, "txt");
         data.NotifyDocumentOutlineUpdated = this.NotifyDocumentOutlineUpdated;
         data.NotifySearchAction = this.NotifyEditorViewSearch;

         // Create the document
         args.Window = new EditorDocumentWindow(data, null) {
            SerializationId = args.Node.SerializationId
         };
      }
   };
   serializer.LoadFromString(layout, dockSite);
}


which works better, but still does not capture all the type of the file created manually.  Is this a reasonable solution? Have I gone off the deep end with hackery? How do I serialize the meaningful state of the document window during the SaveToString call?





Best Regards
Mark Mazurik

[Modified 2 years ago]

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Mark,

Correct, the layout serialization just saves tool windows by default and you need to use that extra DockSiteSerializationBehavior.All option to enable the MDI layout to be saved as well.

The DocumentWindowDeserializationBehavior enum has several options, which are described in this documentation topic.  The default value is to not restore anything about the document window if no match is found in the DockSite.  AutoCreate will generate a stub document window (only if no match is found on the DockSite) and you can initialize it in the DockingWindowDeserializing event, but you will need to set all the aspects of the document window like the FileName and its content.  LazyLoad is kind of a mix of the two where the layout location is saved for unmatched document windows, and if a document window is eventually opened with the same SerializationId, it will be placed in the serialized location.  The option you choose to use depends on your scenario.

We recommend that you use the SerializationId property to uniquely identify the document.  SerializationId is a string, so it could be a filename, URL string, or GUID string, or whatever else helps you identify the document.

Regardless which of the three DocumentWindowDeserializationBehavior options above you choose to go with, the DockingWindowDeserializing event fires for each document window that is being deserialized in the layout.  If an e.Window is available after the event is called (whether we found a match, one was auto-created, or you created one in the event handler), the e.Window will be opened in the DockSite layout in the location as specified by the serialization data.  You can use the SerializationId in your DockingWindowDeserializing event handler to know what document the document window represents, and initialize e.Window and its properties and content appropriately.

I hope that helps!


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.