这是indexloc提供的服务,不要输入任何密码
Skip to content

NullReferenceException while restoring layout with not existing document #38

@Muhahe

Description

@Muhahe

Hi, im using your fork of AD with restore layout as described in this article

While restoring layout, there is possibility that XML layout will contains reference to document which isnt open (while restoring) and in previous version AD ignored it and opened rest of layout.
But in 3.5.6 version it throws NullReferenceException (shown below).

In LayoutContent.cs when it comes to restore not existing document Root.Manager is null (as shown on screenshot)

image

Is there a possibility how to skip restoring documents which are not opened? For restoring layout im using code you wrote on codeproject.com (link in top of issue). Full code example described bellow. I thought, that when i set e.Cancel to true in LayoutSerializationCallback, then layout restoring for this document will be canceled. Or am i missing something?

private void LoadDockingManagerLayout(DockingManager docManager)
        {
            String layoutFileName = Path.Combine(LayoutDir, LayoutFileName);
            layoutFileName = layoutFileName+ ".config";

            if (!File.Exists(layoutFileName))
            {
                return;
            }

            var layoutSerializer = new XmlLayoutSerializer(docManager);

            layoutSerializer.LayoutSerializationCallback += LayoutSerializer_LayoutSerializationCallback;

            try
            {
                layoutSerializer.Deserialize(layoutFileName);
            }
            catch (Exception ex)
            {

                Console.WriteLine("AD Layout deserialization failed: " + ex.ToString());
                //if loading layout fails, restore default layout
                ResetLayout();
            }
            layoutSerializer.LayoutSerializationCallback -= LayoutSerializer_LayoutSerializationCallback;
        }

        private void LayoutSerializer_LayoutSerializationCallback(object sender, LayoutSerializationCallbackEventArgs e)
        {
            // This can happen if the previous session was loading a file
            // but was unable to initialize the view ...
            if (e.Model.ContentId == null)
            {
                e.Cancel = true;
                return;
            }

            ReloadContentOnStartUp(e);
        }

        private void ReloadContentOnStartUp(LayoutSerializationCallbackEventArgs args)
        {
            string sId = args.Model.ContentId;

            // Empty Ids are invalid but possible if aaplication is closed with File>New without edits.
            if (string.IsNullOrWhiteSpace(sId) == true)
            {
             
```   args.Cancel = true;
                return;
            }

            args.Content = ReloadItem(args.Model);

            if (args.Content == null) { 
                args.Cancel = true;                
            }
        }

        private object ReloadItem(object item)
        {
            object ret = null;

            switch (item)
            {
                case LayoutAnchorable anchorable:    
                    //list of tools windows
                    ret = Manager.Tools.FirstOrDefault(i => i.ContentId == anchorable.ContentId);
                    break;
                case LayoutDocument document:
                    // list of restored documents
                    ret = Manager.Documents.FirstOrDefault(i => i.ContentId == document.ContentId);
                    break;
                default:
                    throw new NotImplementedException("Not implemented type of AD item ");
            }

            return ret;
        }


`System.NullReferenceException: Object reference not set to an instance of an object.
   at Xceed.Wpf.AvalonDock.Layout.LayoutContent.CloseInternal() in F:\MyFiles\CSharp\00_GitHub\Avalondock\source\Components\Xceed.Wpf.AvalonDock\Layout\LayoutContent.cs:line 872
   at Xceed.Wpf.AvalonDock.Layout.LayoutDocument.CloseDocument() in F:\MyFiles\CSharp\00_GitHub\Avalondock\source\Components\Xceed.Wpf.AvalonDock\Layout\LayoutDocument.cs:line 181
   at Xceed.Wpf.AvalonDock.Layout.Serialization.LayoutSerializer.FixupLayout(LayoutRoot layout) in F:\MyFiles\CSharp\00_GitHub\Avalondock\source\Components\Xceed.Wpf.AvalonDock\Layout\Serialization\LayoutSerializer.cs:line 125
   at Xceed.Wpf.AvalonDock.Layout.Serialization.XmlLayoutSerializer.Deserialize(TextReader reader) in F:\MyFiles\CSharp\00_GitHub\Avalondock\source\Components\Xceed.Wpf.AvalonDock\Layout\Serialization\XmlLayoutSerializer.cs:line 82
   at Xceed.Wpf.AvalonDock.Layout.Serialization.XmlLayoutSerializer.Deserialize(String filepath) in F:\MyFiles\CSharp\00_GitHub\Avalondock\source\Components\Xceed.Wpf.AvalonDock\Layout\Serialization\XmlLayoutSerializer.cs:line 110
   at Skald.MVVM.QuestSystem.Controls.Docking.LayoutSaveLoadUtil.LoadDockingManagerLayout(DockingManager docManager) in C:\Workspaces\Skald_questSystemUI\Skald\MVVM\QuestSystem\Controls\Docking\LayoutSaveLoadUtil.cs:line 72`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions