Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when deserializing layout or closing a window #1746

Open
stinos opened this issue May 4, 2023 · 4 comments
Open

NullReferenceException when deserializing layout or closing a window #1746

stinos opened this issue May 4, 2023 · 4 comments

Comments

@stinos
Copy link

stinos commented May 4, 2023

I can unfortunately not provide a minimal example to reproduce because this issue happens rarely, so far never on my machine, and any sample application I tried which resembles the actual application does not exhibit it. So here are just stacktraces, which might help seeing the implementation of UpdateContainedFloatingWindowTaskbarTitle is fairly short. Stacktraces shown from the point where my code calls into AvalonDock code.

  System.NullReferenceException: Object reference not set to an instance of an object.
    source = Xceed.Wpf.AvalonDock
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.UpdateContainedFloatingWindowTaskbarTitle(Boolean newValue)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.OnIsSelectedChanged(Boolean oldValue, Boolean newValue)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.set_IsSelected(Boolean value)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.OnParentChanging(ILayoutContainer oldValue, ILayoutContainer newValue)
   at Xceed.Wpf.AvalonDock.Layout.LayoutElement.set_Parent(ILayoutContainer value)
   at Xceed.Wpf.AvalonDock.Layout.LayoutGroup`1._children_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
   at System.Collections.ObjectModel.Collection`1.Remove(T item)
   at Xceed.Wpf.AvalonDock.Layout.LayoutRoot._hiddenAnchorables_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item)
   at Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable.Hide(Boolean cancelable)
   at Xceed.Wpf.AvalonDock.Layout.Serialization.LayoutSerializer.FixupLayout(LayoutRoot layout)

System.NullReferenceException: Object reference not set to an instance of an object.
    source = Xceed.Wpf.AvalonDock
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.UpdateContainedFloatingWindowTaskbarTitle(Boolean newValue)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.OnIsSelectedChanged(Boolean oldValue, Boolean newValue)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.set_IsSelected(Boolean value)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.OnParentChanging(ILayoutContainer oldValue, ILayoutContainer newValue)
   at Xceed.Wpf.AvalonDock.Layout.LayoutElement.set_Parent(ILayoutContainer value)
   at Xceed.Wpf.AvalonDock.Layout.LayoutRoot._hiddenAnchorables_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.RemoveItem(Int32 index)
   at System.Collections.ObjectModel.Collection`1.Remove(T item)
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.CloseInternal()
   at Xceed.Wpf.AvalonDock.Layout.LayoutAnchorable.CloseAnchorable()

Version: 4.5.0. The layout looks like this:

<xcad:DockingManager x:Name="DockManager">
  <layout:LayoutRoot>
    <layout:LayoutPanel Orientation="Horizontal">
      <layout:LayoutAnchorablePaneGroup Orientation="Horizontal" DockWidth="0.3*" >
        <layout:LayoutAnchorablePaneGroup Orientation="Vertical">
          <layout:LayoutAnchorablePane x:Name="LeftPane" DockHeight="0.7*"/>
          <layout:LayoutAnchorablePane x:Name="SubLeftPane" DockHeight="0.3*" />
        </layout:LayoutAnchorablePaneGroup>
      </layout:LayoutAnchorablePaneGroup>
      <layout:LayoutAnchorablePaneGroup Orientation="Vertical" DockWidth="0.7*">
        <layout:LayoutAnchorablePaneGroup Orientation="Horizontal" DockHeight="0.7*">
          <layout:LayoutAnchorablePaneGroup Orientation="Vertical" DockWidth="0.3*">
            <layout:LayoutAnchorablePane x:Name="FookPane" />
            <layout:LayoutAnchorablePane x:Name="BarPane" />
          </layout:LayoutAnchorablePaneGroup>
          <layout:LayoutAnchorablePane x:Name="MainPane" DockWidth="0.7*"/>
        </layout:LayoutAnchorablePaneGroup>
        <layout:LayoutAnchorablePaneGroup Orientation="Horizontal" DockHeight="0.3*" >
          <layout:LayoutAnchorablePane x:Name="BazPane"/>
        </layout:LayoutAnchorablePaneGroup>
      </layout:LayoutAnchorablePaneGroup>
    </layout:LayoutPanel>
  </layout:LayoutRoot>
</xcad:DockingManager>
@stinos stinos changed the title NullReferenceException when altering layout or closing a window NullReferenceException when deserializing layout or closing a window May 4, 2023
@XceedBoucherS
Copy link
Collaborator

Hi @stinos ,
Thank you for reproting this.
Since this issue is hard to reproduce, we can only add more checks in the UpdateContainedFloatingWindowTaskbarTitle() method.

Here's the updated content of the method:
` private void UpdateContainedFloatingWindowTaskbarTitle( bool newValue )
{
if( !newValue ) // LayoutContent is being deselected
{
// Check if LayoutContent is inside a FloatingWindowControl
// And set the correct title for Taskbar Title
var root = this.Root;

    if( (root != null) && (root.Manager != null) )
    {
      var lfwc = root.Manager.FloatingWindows;
      var containedFloatingWindowControl = lfwc.FirstOrDefault( f => (f.Model != null) && f.Model.Descendents().OfType<LayoutContent>().Where( l => l.ContentId == this.ContentId ).FirstOrDefault() != null );

      if( containedFloatingWindowControl != null )
      {
        var selectedLayoutContent = containedFloatingWindowControl.Model.Descendents().OfType<LayoutContent>().Where( l => l.IsSelected ).FirstOrDefault();

        if( selectedLayoutContent != null )
        {
          if( containedFloatingWindowControl.Title != selectedLayoutContent.Title )
          {
            containedFloatingWindowControl.Title = selectedLayoutContent.Title;
          }
        }
      }
    }
  }
}`

This will be updated in the next release.
Thank you

@czh098tom
Copy link

czh098tom commented Jan 21, 2024

On my machine this problem is always reproduced when I tried to deserialize layouts.

Target framework is net8.0-windows and package reference is "Extended.Wpf.Toolkit" Version="4.5.1".

Here is a short example.

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:dock="http://schemas.xceed.com/wpf/xaml/avalondock"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <dock:DockingManager Name="dockingManager">
            <dock:LayoutRoot>
                <dock:LayoutPanel Orientation="Horizontal">
                    <dock:LayoutAnchorablePane>
                        <dock:LayoutAnchorable>
                            <TextBlock>A</TextBlock>
                        </dock:LayoutAnchorable>
                    </dock:LayoutAnchorablePane>
                    <dock:LayoutDocumentPane>
                        <dock:LayoutDocument>
                            <TextBox>X</TextBox>
                        </dock:LayoutDocument>
                    </dock:LayoutDocumentPane>
                </dock:LayoutPanel>
            </dock:LayoutRoot>
        </dock:DockingManager>
        <Button Height="25" Width="50" Click="Button_Click">Z</Button>
    </Grid>
</Window>
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Xceed.Wpf.AvalonDock;
using Xceed.Wpf.AvalonDock.Layout.Serialization;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MemoryStream ms = new();
            var xs = new XmlLayoutSerializer(dockingManager);
            xs.Serialize(ms);
            var str = Encoding.Default.GetString(ms.ToArray());
            Debug.WriteLine(str);

            MemoryStream ms2 = new(Encoding.Default.GetBytes(str));
            xs.Deserialize(ms2);
        }
    }
}

The serialized string written in debug view is

<?xml version="1.0" encoding="utf-8"?>
<LayoutRoot>
  <RootPanel Orientation="Horizontal">
    <LayoutAnchorablePane>
      <LayoutAnchorable AutoHideMinWidth="100" AutoHideMinHeight="100" IsSelected="True" CanClose="False" />
    </LayoutAnchorablePane>
    <LayoutDocumentPane>
      <LayoutDocument IsSelected="True" IsLastFocusedDocument="True" CanClose="True" LastActivationTimeStamp="01/21/2024 16:27:25" />
    </LayoutDocumentPane>
  </RootPanel>
  <TopSide />
  <RightSide />
  <LeftSide />
  <BottomSide />
  <FloatingWindows />
  <Hidden />
</LayoutRoot>

@XceedBoucherS
Copy link
Collaborator

Hi @czh098tom,

Thank you for your example.
Yes, I can reproduce the issue with your sample. But this is already fixed in the latest release, v4.6, which is already available for the user of the Plus Version. This fix will be available in the Community Toolkit version v4.6, which should be released this week.
Thank you

@stinos
Copy link
Author

stinos commented Mar 15, 2024

This seems to be fixed for us, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants