[Version 2.0] Bug? Deserializing floating window results in two views

Dec 3, 2012 at 11:42 PM

I've got a weird problem here and I'm not sure if it's a bug or a problem in my code.

i'm using the following code to serialize the layout into my app settings:

using (var memoryStream = new MemoryStream()) {
	new XmlLayoutSerializer(MainWindow.sharedDockingManager).Serialize(memoryStream);
	memoryStream.Seek(0, SeekOrigin.Begin);
	var layout = new StreamReader(memoryStream).ReadToEnd();
	Settings.Default.AvalonDockLayout = layout;
}

And this to deserialize:

string layout = Settings.Default.AvalonDockLayout;
var bytes = Encoding.Default.GetBytes(layout);
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(bytes, 0, bytes.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(MainWindow.sharedDockingManager);
layoutSerializer.LayoutSerializationCallback += (s, e) =>
{
	if (!string.IsNullOrWhiteSpace(e.Model.ContentId))
		e.Content =
			Workspace.This.AvalonDockAdapter.OpenLayoutItemByContentId(
				e.Model.ContentId);
};
layoutSerializer.Deserialize(memoryStream);

Where the callback uses this helper method to get or create a viewModel:

case "CommoditySearchToolWindow":
    var commoditySearchToolWindowViewModel =
        Workspace.This.AvalonDockAdapter.GetAnchorableOfType(typeof(CommoditySearchToolWindowViewModel));
    if (commoditySearchToolWindowViewModel != null)
        return commoditySearchToolWindowViewModel;

    commoditySearchToolWindowViewModel = new CommoditySearchToolWindowViewModel();
    Workspace.This.AvalonDockAdapter.AddAnchorable(commoditySearchToolWindowViewModel, false);
    return commoditySearchToolWindowViewModel;

This works perfectly for docked AnchorablePanes, but if a pane is floating when it's serialized, upon deserialization it produces a window containing my view and hooked up to the viewmodel correctly, and another directly behind it in the z-index (the same size) with the same properties (title, etc) but no view. Clicking the X on one of the windows will hide both, and reopening one will open both again. Docking either of the floating windows to the main window will cause both to dissapear and the correct one (with the view) to dock, and then floating the now docked pane results in correction of the problem with only the one window floating.

Using version 2.0.1737.0 built from source.

Dec 4, 2012 at 7:54 AM

Hi,
much probably you're adding two times the same contentid (one to the anchorables collection and one returned to the callback handler). Said that I'm sure there is also a bug somewhere that shows the two floating windows.

It would be really useful if you could wrap a really small sample project that shows the problem,
Thanks,

Ado

Dec 4, 2012 at 4:12 PM

Hi Ado,

Yes I think you're correct about it being added twice, but only one goes into the collection;

When the LayoutSerializationCallback fires, calling OpenLayoutItemByContentId, that checks if the anchorable already exists in the collection, if so it simply returns the existing one, otherwise it adds a new one to the collection and returns that.

Is it possible that the layout serializer is creating a window for the returned viewmodel instance but no view  but also creating a window+view for the viewmodel in the collection?

I'll happily throw something together using my working code and send it to you in the next few days.

 

Current workaround for this is to dock everything that's floating before serialization (this is ugly code I know):

foreach (LayoutFloatingWindow floatingWindow in dockingManager.Layout.FloatingWindows.ToList())
    foreach (LayoutAnchorablePaneGroup anchorablePaneGroup in floatingWindow.Children.ToList())
        foreach (LayoutAnchorablePane anchorablePane in anchorablePaneGroup.Children.ToList())
            foreach (LayoutAnchorable anchorable in anchorablePane.Children.ToList())
                anchorable.Dock();

Cheers,

Tim

Dec 13, 2012 at 4:36 PM

Hi,

I've fixed this bug in latest build, thanks,

Ado

Dec 13, 2012 at 4:58 PM
Thanks! Appreciate the quick fix :)


-
Tim Hoskins
Jan 17, 2013 at 1:47 PM

Sorry to bump into this, i ran into the same issue. Where can i get the latest build? (on the main page there are no downloads after Dec. 4)