Save / Restore window layout

Docking/MDI for WPF Forum

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was before this thread was created.
Posted 1 month ago by Neil Larson
Version: 19.1.0683
Avatar

I am hoping to be able to save/restore the arrangement of the windows.

Here is the setup of the DockSite.

      <docking:DockSite x:Name="dockSite" CanDocumentWindowsFloat="True" FloatingWindowTitle="Mongoose Editor" 
                        AreDocumentWindowsDestroyedOnClose="False" 
                        WindowsClosing="dockSite_WindowsClosing" Loaded="dockSite_Loaded"  >
         <docking:SplitContainer>
            <docking:Workspace Name="dockingWorkspace">
               <docking:TabbedMdiHost x:Name="tabbedMdiHost" TabStripPlacement="Bottom">
                  <docking:TabbedMdiContainer x:Name="tabbedMdiContainer">
                     <docking:DocumentWindow x:Name="CommandWindow" Title="Commands" >

Here is the serialize/deserialize code.

      private string GetDockSiteLayoutPath()
      {
         var dir = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\MongooseEditor";
         return $"{dir}\\Layout_{dockSite.Name}.xml";
      }

      private void LoadDockSiteLayout(DockSite dockSite)
      {
         if (dockSite == null)
            return;

         var serializer = new DockSiteLayoutSerializer
         {
            DocumentWindowDeserializationBehavior = DockingWindowDeserializationBehavior.AutoCreate,
            SerializationBehavior = DockSiteSerializationBehavior.All,
            ToolWindowDeserializationBehavior = DockingWindowDeserializationBehavior.AutoCreate,
         };

         var path = GetDockSiteLayoutPath();
         if (File.Exists(path))
            serializer.LoadFromFile(path, dockSite);
      }

      private void SaveDockSiteLayout(DockSite dockSite)
      {
         if (dockSite == null)
            return;

         var serializer = new DockSiteLayoutSerializer
         {
            DocumentWindowDeserializationBehavior = DockingWindowDeserializationBehavior.AutoCreate,
            SerializationBehavior = DockSiteSerializationBehavior.All,
            ToolWindowDeserializationBehavior = DockingWindowDeserializationBehavior.AutoCreate,
         };

         var path = GetDockSiteLayoutPath();
         serializer.SaveToFile(path, dockSite);
      }

      private void dockSite_WindowsClosing(object sender, DockingWindowsEventArgs e)
      {
         SaveDockSiteLayout(sender as DockSite);
      }

      private void dockSite_Loaded(object sender, RoutedEventArgs e)
      {
         LoadDockSiteLayout(sender as DockSite);
      }

and the serializer seems to only save the default layout without any information about the document windows docked location or size. It just shows the windows as all filling the workspace.  

<DockSiteLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SerializationFormat="All" Version="2">
	<AutoHideHost />
	<Content xsi:type="Workspace">
		<Content xsi:type="TabbedMdiHost">
			<Content xsi:type="TabbedMdiContainer" DockedSize="1583,1153" SelectedWindowUniqueId="7674065c-c937-4fdb-b58c-526dbc7f985d">
				<UIElement xsi:type="DocumentWindowRef" UniqueId="7674065c-c937-4fdb-b58c-526dbc7f985d" />
				<UIElement xsi:type="DocumentWindowRef" UniqueId="dc2625e8-5e2e-4a5c-b64c-c2839a3f9e95" />
				<UIElement xsi:type="DocumentWindowRef" UniqueId="aa4e7c86-f51b-4670-b02d-149fb8297e87" />
			</Content>
		</Content>
	</Content>
	<DocumentWindows>
		<DocumentWindow UniqueId="7674065c-c937-4fdb-b58c-526dbc7f985d" Name="CommandWindow" ContainerDockedSize="1583,1153" IsOpen="true" State="Document" />
		<DocumentWindow UniqueId="dc2625e8-5e2e-4a5c-b64c-c2839a3f9e95" Name="UnitsUsedWindow" ContainerDockedSize="1583,1153" IsOpen="true" State="Document" />
		<DocumentWindow UniqueId="aa4e7c86-f51b-4670-b02d-149fb8297e87" Name="BugCreationTab" ContainerDockedSize="1583,1153" IsOpen="true" State="Document" />
	</DocumentWindows>
</DockSiteLayout>

I had actually docked the UnitsUsedWindow below the CommandWindow before serialization.

Thanks for the help.

Comments (2)

Posted 1 month ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Neil,

When I test two tabbed MDI documents above and below each other in our Layout Serialization QuickStart, I see a SplitContainer with two TabbedMdiContainers in it, all within the TabbedMdiHost.   Are you positive you saved the layout after you did the layout change to make two tabbed MDI containers?

If so, please tell us how to reproduce it with that sample, or make a new simple sample project with your code above that shows it happening and send that to our support address, referencing this thread.  Be sure to exclude the bin/obj folders from the ZIP of any sample you send so it doesn't get spam blocked.  Thanks!


Actipro Software Support

Answer - Posted 1 month ago by Neil Larson
Avatar

DockSite_WindowsClosing occurs before the DockSite_Loaded, so the file was being overwritten with the defaults before it was read to arrange the windows. Im not sure why there is a WindowsClosing event coming up first, but the fix was to save the layout on the main window closing instead.

Add Comment

Please log in to a validated account to post comments.