仅具有LayoutDocumentPane的简单AvalonDock应用程序。


XAML:

<Window x:Name="MainWindow1" x:Class="AvalonTest2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:xcad="http://schemas.xceed.com/wpf/xaml/avalondock"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow1_Loaded">
    <Grid Name="MainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ToolBar Grid.Row="0" Grid.Column="0">
            <Button Content="New Tab" Margin="10,0,0,0" Click="NewTabClick"/>
            <Button Content="Save layout" Margin="10,0,0,0" Click="Button_Click"/>
        </ToolBar>

        <xcad:DockingManager x:Name="dockManager" Grid.Row="1">
            <xcad:LayoutRoot x:Name="_layoutRoot">
                <xcad:LayoutPanel Orientation="Horizontal" x:Name="_layoutPanel">
                    <xcad:LayoutDocumentPane x:Name="workSpace">
                        <xcad:LayoutDocument ContentId="dummy" Title="Dummy" >
                            <Button x:Name="dummyButton" Content="Dummy Content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </xcad:LayoutDocument>
                    </xcad:LayoutDocumentPane>
                </xcad:LayoutPanel>
            </xcad:LayoutRoot>
        </xcad:DockingManager>
    </Grid>
</Window>


码:

using System;
using System.IO;
using System.Windows;
using Xceed.Wpf.AvalonDock.Layout;
using Xceed.Wpf.AvalonDock.Layout.Serialization;

namespace AvalonTest2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void NewTabClick(object sender, RoutedEventArgs e)
        {
            var ld = new LayoutDocument();
            var x = new Random().Next().ToString();
            ld.Title = x;
            ld.ContentId = x;
             workSpace.Children.Add(ld);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var layoutSerial = new XmlLayoutSerializer(dockManager);
            layoutSerial.Serialize("layout.xml");
        }

        private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
        {
            if (File.Exists("layout.xml"))
            {
               // var serializer = new XmlLayoutSerializer(dockManager);
               // serializer.Deserialize("layout.xml");
            }
        }
    }
}


我按“新建标签”按钮,一切正常:出现新标签。

我重新启动程序,保存布局(“保存布局”按钮),关闭程序并删除反序列化行上的注释。

我启动程序,然后按“新建选项卡”,没有选项卡出现。我将在LayoutDocumentPane(workSpace)内查看,“ Children”属性充满了新的Documents。

怎么了?

最佳答案

不幸的是,(反)序列化功能在AvalonDock套件中实现的不是很好。

反序列化时,将创建一个全新的LayoutRoot对象。您在XAML中定义一个名称为LayoutDocumentPaneworkSpace,并将新创建的LayoutDocument添加到此窗格中。但是,在反序列化之后,该LayoutDocumentPane不再属于布局-已被删除;而是创建了一个新的LayoutDocumentPane

因此,您看不到添加到LayoutDocument容器中的任何workSpace视图。

不要按名称引用您的LayoutDocumentPane实例,而是动态查找它。您必须将代码更改为:

var ld = new LayoutDocument();
var x = new Random().Next().ToString();
ld.Title = x;
ld.ContentId = x;

LayoutDocumentPane documentPane = this.dockManager.Layout.Descendents().OfType<LayoutDocumentPane>().SingleOrDefault();
if (documentPane != null)
{
    documentPane.Children.Add(ld);
}

07-24 16:51