Search code examples
wpfmvvmmefcaliburn.microavalondock

MVVM Application not Restoring State Correctly


I have built a class library that acts as a GUI framework that can be inherited by other projects. This application is based on projects Wild and Gemini.

My problem is that upon restoring Avalon Dock's layout using the standard serializer

var layoutSerializer = new XmlLayoutSerializer(manager);

where manager is type DockingManager. The manager restores and empty tab. My guess is that Caliburn Micro cannot find the stored ViewModel (named HomeViewModel). However, I am struggling to confirm this.

I believe my bootstrapper to be correct and that the MEF containers are being setup correctly to allow resolution of external types. I have debugged the project to a point where I think this issue is occurring and in the output window I can see Attach(Home) where the attach is occurring (note, "Home" is the display name of the HomeViewModel). However, I don't know what is wrong with the attach process as this is handled by MEF/Caliburn.

I am really stuck with debugging this an wondered if

  1. Any one could offer any insightful advice as to how to proceed with the debugging process?
  2. Anyone would be willing to take a look at the solution?

I have spent a hell of a lot of time debugging this without any luck and the problem is sufficiently esoteric and illusive as to render most posts here irrelevant to me.

Thanks for your time.


Solution

  • as discussed and after looking on the sample code provided, I understand that the following

    • HomeViewModel or can say LayoutItemBase is not supposed to be reopened as ShouldReopenOnStartup is set to false
    • if you close the application while leaving a document open for HomeViewModel it is restored on next start with blank view [Not OK]

    Analysis

    the SaveState method was correctly honoring ShouldReopenOnStartup value and was not emitting the state for the HomeViewModel but dock manager was still emitting an element for the document.

    So upon next restart the LoadState does not find any stored state but a window was created as an element was present in the dock manager's layout state

      <LayoutDocument Title="HomePP" IsSelected="True" IsLastFocusedDocument="True" ContentId="d716f824-cfff-4b54-8fd6-2d026a99369a" .../>
    

    you did try to use e.Cancel property of Serialization callback to cancel the event, but seems like it is not supposed to prevent of loading a window but just simply to cancel the event if not needed.

    Resolution

    So the ideal approach is to close the documents which are not supposed to be restored before saving the layout

    here is how I did

    ShellViewmodel.cs : Line 279 method SaveState(string)

    change the following code

        if (!item.ShouldReopenOnStartup)
            continue;
    

    to

        if (!item.ShouldReopenOnStartup)
        {
            //this item is not supposed to be restored so close the window before saving layout
            IDocument doc = item as IDocument;
            if (doc != null)
                CloseDocument(doc);
            continue;
        }