xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.9k stars 878 forks source link

NullReferenceException when deserializing layout or closing a window #1746

Open stinos opened 1 year ago

stinos commented 1 year ago

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>
XceedBoucherS commented 1 year ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 8 months ago

This seems to be fixed for us, thanks!