Articles in this section
Category / Section

How to serialize the dynamically added children in Docking Manager?

2 mins read

In Docking Manager, the children that are added during run time were not serialized automatically. It can be manually serialized and de-serialized as follows.

Serializing the children

In Window Closing event, Call SaveDockState() method to save the state of static children. Then create a list of string, add the header of all dynamic children and then write it to the xml file.

Deserializing the children

In DockingManager Loaded event, extract the list of headers from xml file. Create child element, set its header and add it to DockingManager. Then call LoadeDockState() method to load all the children.

Here is an example of serializing the children manually using XML Serializer:

MainWindow.xaml

<syncfusion:DockingManager x:Name="docking" UseDocumentContainer="True">
            <Border syncfusion:DockingManager.Header="Window 1" syncfusion:DockingManager.State="Document"/>
</syncfusion:DockingManager>

MainWindow.xaml.cs

public partial class MainWindow : Window
    {
        private int count = 1;
        string filepath = System.IO.Path.GetTempPath() + "Dockstate.xml";
        private static readonly Dictionary<Type, XmlSerializer> _xmlSerializerCache = new Dictionary<Type, XmlSerializer>();
        private readonly string m_StoreFileName = AppDomain.CurrentDomain.FriendlyName + ".dat";
        public MainWindow()
        {
            InitializeComponent();
            docking.Loaded += dockingManager_Loaded;
            Closing += MainWindow_Closing;
            (docking.DocContainer as DocumentContainer).Loaded += DocContainer _Loaded;
        }
        void DocContainer _Loaded(object sender, RoutedEventArgs e)
        {
            DocumentTabControl tabControl = VisualUtils.FindDescendant(docking.DocContainer as DocumentContainer, typeof (DocumentTabControl)) as DocumentTabControl;
            if (tabControl != null)
            {
                tabControl.IsNewButtonEnabled = true;
                tabControl.NewButtonClick += tabControl_NewButtonClick;
            }
        }
        void tabControl_NewButtonClick(object sender, EventArgs e)
        {
            ContentControl control = new ContentControl();
            DockingManager.SetHeader(control, "Window " + ++count);
            DockingManager.SetState(control, DockState.Document);
            docking.Children.Add(control);
        }
        void dockingManager_Loaded(object sender, RoutedEventArgs e)
        {
            Stream stream;
            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
                if (isoStorage!=null && !string.IsNullOrEmpty(m_StoreFileName)
                    && 0 < isoStorage.GetFileNames(m_StoreFileName).Length)
                {
                    stream = new IsolatedStorageFileStream(m_StoreFileName, FileMode.OpenOrCreate, isoStorage);
                    try
                    {
                        XmlTextReader xmlTextReader = new XmlTextReader(stream);
                        LoadState(xmlTextReader);
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
            }
            catch (Exception)
            { }
            if (File.Exists(filepath))
                docking.LoadDockState(filepath);
        }
        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            docking.SaveDockState(filepath);
            IsolatedStorageFile isoStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            if (null != isoStorage && !string.IsNullOrEmpty(m_StoreFileName))
            {
                Stream stream = new IsolatedStorageFileStream(m_StoreFileName, FileMode.Create, isoStorage);
                try
                {
                    XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
                    WriteDateToWriter(writer);
                    writer.Flush();
                }
                finally
                {
                    stream.Close();
                }
            }
        }
        private bool LoadState(XmlReader reader)
        {
            XmlSerializer serializer = CreateDefaultXmlSerializer(typeof(List<string>));
 
            if (serializer.CanDeserialize(reader))
            {
                List<string> dockingParamsList = (List<string>)serializer.Deserialize(reader);
                foreach (var header in dockingParamsList)
                {
                    ContentControl control = new ContentControl();
                    DockingManager.SetHeader(control, header);
                    docking.Children.Add(control);
                    count = Int32.Parse(header[header.Length - 1].ToString());
                }
            }
            return true;
        }
        private void WriteDateToWriter(XmlWriter writer)
        {
            List<string> dockingParamsList = new List<string>();
            foreach (var child in docking.Children)
            {
                if (child is ContentControl)
                {
                    if (!dockingParamsList.Contains(DockingManager.GetHeader(child as DependencyObject).ToString()))
                        dockingParamsList.Add(DockingManager.GetHeader(child as DependencyObject).ToString());
                }
            }
            XmlSerializer serializer = CreateDefaultXmlSerializer(typeof(List<string>));
            serializer.Serialize(writer, dockingParamsList);
        }
        public static XmlSerializer CreateDefaultXmlSerializer(Type type)
        {
            XmlSerializer serializer;
            if (_xmlSerializerCache.TryGetValue(type, out serializer))
            {
                return serializer;
            }
            else
            {
                var importer = new XmlReflectionImporter();
                var mapping = importer.ImportTypeMapping(type, null, null);
                serializer = new XmlSerializer(mapping);
                return _xmlSerializerCache[type] = serializer;
            }
        }
    }

 

Samples

C#: DockingManager_Serialization_C#

VB: DockingManager_Serialization_VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied