Example #1
0
        /// <summary>
        /// 保存初始布局,同步处理布局、提取Tab字典,已调优
        /// </summary>
        void SaveDefaultXml()
        {
            _owner.IsReseting = true;

            // 按类型提取各项
            List <Main> centers    = new List <Main>();
            List <Pane> lefts      = new List <Pane>();
            List <Pane> rights     = new List <Pane>();
            List <Pane> topBottom  = new List <Pane>();
            List <Pane> floats     = new List <Pane>();
            List <Tab>  leftHide   = new List <Tab>();
            List <Tab>  rightHide  = new List <Tab>();
            List <Tab>  topHide    = new List <Tab>();
            List <Tab>  bottomHide = new List <Tab>();

            while (_owner.Items.Count > 0)
            {
                object obj = _owner.Items[0];
                _owner.Items.RemoveAt(0);

                if (obj is Pane di)
                {
                    ExtractItems(di);
                    if (di.Pos == PanePosition.Floating)
                    {
                        floats.Add(di);
                    }
                    else
                    {
                        // 在停靠中挑出自动隐藏项
                        foreach (Tab tab in GetHideItems(di))
                        {
                            if (di.Pos == PanePosition.Left)
                            {
                                leftHide.Add(tab);
                            }
                            else if (di.Pos == PanePosition.Right)
                            {
                                rightHide.Add(tab);
                            }
                            else if (di.Pos == PanePosition.Top)
                            {
                                topHide.Add(tab);
                            }
                            else if (di.Pos == PanePosition.Bottom)
                            {
                                bottomHide.Add(tab);
                            }
                        }

                        if (!IsRemoved(di))
                        {
                            RemoveUnused(di);

                            if (di.Pos == PanePosition.Left)
                            {
                                lefts.Add(di);
                            }
                            else if (di.Pos == PanePosition.Right)
                            {
                                rights.Add(di);
                            }
                            else
                            {
                                topBottom.Add(di);
                            }
                        }
                    }
                }
                else if (obj is Main center)
                {
                    ExtractItems(center);
                    centers.Add(center);
                }
                else
                {
                    // 包含普通界面元素时:
                    // 1. 将其放于主区
                    // 2. 不显示标题栏
                    // 3. 不自动保存布局状态
                    // 4. 不显示恢复默认布局按钮
                    // 5. 一般为单视图窗口
                    _owner.AutoSaveLayout = false;
                    Main wc   = new Main();
                    Tabs tabs = new Tabs {
                        ShowHeader = false
                    };
                    tabs.Items.Add(new Tab {
                        Content = obj
                    });
                    wc.Items.Add(tabs);
                    centers.Add(wc);
                }
            }

            StringBuilder xml = new StringBuilder();

            using (XmlWriter writer = XmlWriter.Create(xml, _writerSettings))
            {
                writer.WriteStartElement("Items");

                // 停靠项,按左右上下顺序输出
                writer.WriteStartElement("Win");
                _colsWidth = new List <double>();
                // 首先Center宽度
                _colsWidth.Add(centers.Count > 0 ? _centerWidth : 0);
                if (lefts.Count > 0)
                {
                    foreach (var di in lefts)
                    {
                        WriteWinItem(di, writer);
                        _owner.Items.Add(di);
                        _owner.RootPanel.Children.Add(di);
                        _colsWidth.Add(di.InitWidth);
                    }
                }
                if (rights.Count > 0)
                {
                    foreach (var di in rights)
                    {
                        WriteWinItem(di, writer);
                        _owner.Items.Add(di);
                        _owner.RootPanel.Children.Add(di);
                        _colsWidth.Add(di.InitWidth);
                    }
                }
                if (topBottom.Count > 0)
                {
                    foreach (var di in topBottom)
                    {
                        WriteWinItem(di, writer);
                        _owner.Items.Add(di);
                        _owner.RootPanel.Children.Add(di);
                    }
                }
                writer.WriteEndElement();

                // 中部项
                if (centers.Count > 0)
                {
                    writer.WriteStartElement("Center");
                    foreach (Main center in centers)
                    {
                        WriteCenter(writer, center.Items);
                        // 挪到CenterItem,特殊处理!
                        while (center.Items.Count > 0)
                        {
                            var centerItem = center.Items[0];
                            center.Items.RemoveAt(0);
                            _owner.CenterItem.Items.Add(centerItem);
                        }
                    }
                    writer.WriteEndElement();
                }

                // 左侧隐藏项
                if (leftHide.Count > 0)
                {
                    writer.WriteStartElement("Left");
                    if (_owner.LeftAutoHide == null)
                    {
                        _owner.CreateLeftAutoHideTab();
                    }
                    foreach (Tab tab in leftHide)
                    {
                        WriteTab(tab, writer);
                        _owner.LeftAutoHide.Unpin(tab);
                    }
                    writer.WriteEndElement();
                }

                // 右侧隐藏项
                if (rightHide.Count > 0)
                {
                    writer.WriteStartElement("Right");
                    if (_owner.RightAutoHide == null)
                    {
                        _owner.CreateRightAutoHideTab();
                    }
                    foreach (Tab tab in rightHide)
                    {
                        WriteTab(tab, writer);
                        _owner.RightAutoHide.Unpin(tab);
                    }
                    writer.WriteEndElement();
                }

                // 上侧隐藏项
                if (topHide.Count > 0)
                {
                    writer.WriteStartElement("Top");
                    if (_owner.TopAutoHide == null)
                    {
                        _owner.CreateTopAutoHideTab();
                    }
                    foreach (Tab tab in topHide)
                    {
                        WriteTab(tab, writer);
                        _owner.TopAutoHide.Unpin(tab);
                    }
                    writer.WriteEndElement();
                }

                // 下侧隐藏项
                if (bottomHide.Count > 0)
                {
                    writer.WriteStartElement("Bottom");
                    if (_owner.BottomAutoHide == null)
                    {
                        _owner.CreateBottomAutoHideTab();
                    }
                    foreach (Tab tab in bottomHide)
                    {
                        WriteTab(tab, writer);
                        _owner.BottomAutoHide.Unpin(tab);
                    }
                    writer.WriteEndElement();
                }

                // 浮动项
                if (floats.Count > 0)
                {
                    writer.WriteStartElement("Float");
                    foreach (Pane wi in floats)
                    {
                        WriteWinItem(wi, writer);
                        OpenInWindow(wi);
                    }
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.Flush();
            }
            _default          = xml.ToString();
            _owner.IsReseting = false;
        }