avalondock deserialization displays nothing from config file

30 Views Asked by At

I have this dockingManager in my xaml. It works well, I can navigate between the different controls, use them and link them to commands and fields with the binding.

I've recently implemented layout saving, based on the MVVMTestApp example in the project's github repository. I've managed to save, or at least to obtain an xml file that seems to be consistent with the layout I've saved, but I can't load it. The program doesn't crash, there's no error, but it doesn't change the layout loaded, the windows already open don't move.

<DockingManager       
x:Name="dockManager"
Grid.Row="1"
AnchorablesSource="{Binding Tools}"
DocumentsSource="{Binding Panes}"
Theme="{Binding SelectedTheme.Item2}">
    <DockingManager.LayoutItemTemplateSelector>
        <local:PanesTemplateSelector>
            <local:PanesTemplateSelector.LoggerViewTemplate>
                <DataTemplate>
                    <tools:LoggerUserControl/>
                </DataTemplate>
            </local:PanesTemplateSelector.LoggerViewTemplate>
            <local:PanesTemplateSelector.LiveMonitorViewTemplate>
                <DataTemplate>
                    <tools:LiveMonitorUserControl/>
                </DataTemplate>
            </local:PanesTemplateSelector.LiveMonitorViewTemplate>
            <local:PanesTemplateSelector.OscilloscopeViewTemplate>
                <DataTemplate>
                    <tools:OscilloscopeUserControl/>
                </DataTemplate>
            </local:PanesTemplateSelector.OscilloscopeViewTemplate>
            <local:PanesTemplateSelector.EventTriggerViewTemplate>
                <DataTemplate>
                    <tools:EventTriggerMainUserControl/>
                </DataTemplate>
            </local:PanesTemplateSelector.EventTriggerViewTemplate>
            <local:PanesTemplateSelector.AlarmManangerViewTemplate>
                <DataTemplate>
                    <tools:AlarmManagerUserControl/>
                </DataTemplate>
            </local:PanesTemplateSelector.AlarmManangerViewTemplate>
            <local:PanesTemplateSelector.ConsoleViewTemplate>
                <DataTemplate>
                    <tools:ConsoleUserControl/>
                </DataTemplate>
            </local:PanesTemplateSelector.ConsoleViewTemplate>
        </local:PanesTemplateSelector>
    </DockingManager.LayoutItemTemplateSelector>

    <DockingManager.LayoutItemContainerStyleSelector>
        <local:PanesStyleSelector>
            <local:PanesStyleSelector.ToolStyle>
                <Style TargetType="{x:Type LayoutAnchorableItem}">
                    <Setter Property="Title" Value="{Binding Model.Title}" />
                    <Setter Property="CanClose" Value="True" />
                    <Setter Property="IconSource" Value="{Binding Model.IconSource}" />
                    <Setter Property="ContentId" Value="{Binding Model.ContentId}" />
                    <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}" />
                    <Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}" />
                </Style>
            </local:PanesStyleSelector.ToolStyle>
            <local:PanesStyleSelector.OscilloscopeStyle>
                <Style TargetType="{x:Type LayoutItem}">
                    <Setter Property="Title" Value="{Binding Model.Title}"/>
                </Style>
            </local:PanesStyleSelector.OscilloscopeStyle>
        </local:PanesStyleSelector>
    </DockingManager.LayoutItemContainerStyleSelector>

    <DockingManager.LayoutUpdateStrategy>
        <local:LayoutInitializer />
    </DockingManager.LayoutUpdateStrategy>

    <LayoutRoot>
        <LayoutPanel Orientation="Vertical">
            <LayoutDocumentPane />
            <LayoutAnchorablePane Name="ToolsPane" DockHeight="150" />
        </LayoutPanel>
    </LayoutRoot>
</DockingManager>

The TestLoadBinding.config file I'm trying to load and which I created earlier saves the layout

<?xml version="1.0" encoding="utf-8"?>
<LayoutRoot>
  <RootPanel Orientation="Horizontal">
    <LayoutPanel Orientation="Vertical">
      <LayoutDocumentPane />
      <LayoutAnchorablePane Id="589a7790-671e-455c-a5ba-4b9aa7b5b9d5" Name="ToolsPane" DockHeight="150">
        <LayoutAnchorable AutoHideMinWidth="100" AutoHideMinHeight="100" Title="Event Trigger" IsSelected="True" CanClose="True" LastActivationTimeStamp="12/21/2023 12:01:09" />
      </LayoutAnchorablePane>
    </LayoutPanel>
    <LayoutAnchorablePaneGroup Orientation="Horizontal" DockWidth="792" FloatingWidth="1594" FloatingHeight="160" FloatingLeft="1152" FloatingTop="563">
      <LayoutAnchorablePane DockHeight="150" FloatingWidth="1594" FloatingHeight="160" FloatingLeft="1152" FloatingTop="563">
        <LayoutAnchorable AutoHideMinWidth="100" AutoHideMinHeight="100" Title="Alarm Manager" IsSelected="True" FloatingLeft="1152" FloatingTop="563" FloatingWidth="1594" FloatingHeight="160" CanClose="True" LastActivationTimeStamp="12/21/2023 12:01:12" PreviousContainerId="589a7790-671e-455c-a5ba-4b9aa7b5b9d5" PreviousContainerIndex="1" />
      </LayoutAnchorablePane>
    </LayoutAnchorablePaneGroup>
  </RootPanel>
  <TopSide />
  <RightSide />
  <LeftSide />
  <BottomSide />
  <FloatingWindows />
  <Hidden />
</LayoutRoot>

the method the command calls to load the layout. Here it works with binding, but I'd tried accessing the variable directly, but there was always the same problem.

private void OnLoadLayoutMenuItem(object dockManager)
{
    var MyLayoutSerializer = new XmlLayoutSerializer((DockingManager)dockManager);
    MyLayoutSerializer.Deserialize($"./AvalonDockLayouts/TestLoadBinding.config");
}

the method for saving

private void OnSaveAsLayout(object dockManager)
{
    string enterSaveAsfileName = Interaction.InputBox("Enter layout name", "Save layout", "DefaultLayoutName");
    var serializer = new XmlLayoutSerializer((DockingManager)dockManager);
    serializer.Serialize($"./AvalonDockLayouts/{"TestLoadBinding"}.config");
    _LayoutName = enterSaveAsfileName;
}

To do all this, I used the MVVMTestApp example from the official AvalonDock project on github. In the code itself, I tried to make it as identical as possible, but noticed a difference at runtime.

If I put a breakpoint on the LayoutSerializer.deserialize line, I get an empty previousAnchorables array: My empty previousAnchorables array

However, this is not the case on the project side: project exemple layoutSerializer

Would it be possible to have the layout in the datacontext class and manage it yourself? Or how do I load my old layout?

thank you for your reply!

0

There are 0 best solutions below