Exemplo n.º 1
0
        protected virtual void FixupLayout(LayoutRoot layout)
        {
            //fix container panes
            foreach (
                var lcToAttach in
                layout.Descendents().OfType <ILayoutPreviousContainer>().Where(lc => lc.PreviousContainerId != null))
            {
                var paneContainerToAttach =
                    layout.Descendents()
                    .OfType <ILayoutPaneSerializable>()
                    .FirstOrDefault(lps => lps.Id == lcToAttach.PreviousContainerId);
                if (paneContainerToAttach == null)
                {
                    throw new ArgumentException($"Unable to find a pane with id ='{lcToAttach.PreviousContainerId}'");
                }

                lcToAttach.PreviousContainer = paneContainerToAttach as ILayoutContainer;
            }


            //now fix the content of the layoutcontents
            foreach (
                var lcToFix in layout.Descendents().OfType <LayoutAnchorable>().Where(lc => lc.Content == null).ToArray()
                )
            {
                LayoutAnchorable previousAnchorable = null;
                if (lcToFix.ContentId != null)
                {
                    //try find the content in replaced layout
                    previousAnchorable = _previousAnchorables.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);
                }

                if (LayoutSerializationCallback != null)
                {
                    var args = new LayoutSerializationCallbackEventArgs(lcToFix, previousAnchorable?.Content);
                    LayoutSerializationCallback(this, args);
                    if (args.Cancel)
                    {
                        lcToFix.Close();
                    }
                    else if (args.Content != null)
                    {
                        lcToFix.Content = args.Content;
                    }
                    else if (args.Model.Content != null)
                    {
                        lcToFix.Hide(false);
                    }
                }
                else if (previousAnchorable == null)
                {
                    lcToFix.Hide(false);
                }
                else
                {
                    lcToFix.Content    = previousAnchorable.Content;
                    lcToFix.IconSource = previousAnchorable.IconSource;
                }
            }


            foreach (
                var lcToFix in layout.Descendents().OfType <LayoutDocument>().Where(lc => lc.Content == null).ToArray())
            {
                LayoutDocument previousDocument = null;
                if (lcToFix.ContentId != null)
                {
                    //try find the content in replaced layout
                    previousDocument = _previousDocuments.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);
                }

                if (LayoutSerializationCallback != null)
                {
                    var args = new LayoutSerializationCallbackEventArgs(lcToFix, previousDocument?.Content);
                    LayoutSerializationCallback(this, args);

                    if (args.Cancel)
                    {
                        lcToFix.Close();
                    }
                    else if (args.Content != null)
                    {
                        lcToFix.Content = args.Content;
                    }
                    else if (args.Model.Content != null)
                    {
                        lcToFix.Close();
                    }
                }
                else if (previousDocument == null)
                {
                    lcToFix.Close();
                }
                else
                {
                    lcToFix.Content = previousDocument.Content;
                }
            }


            layout.CollectGarbage();
        }
        protected virtual void FixupLayout(LayoutRoot layout)
        {
            //fix container panes
            foreach (var lcToAttach in layout.Descendents().OfType<ILayoutPreviousContainer>().Where(lc => lc.PreviousContainerId != null))
            {
                var paneContainerToAttach = layout.Descendents().OfType<ILayoutPaneSerializable>().FirstOrDefault(lps => lps.Id == lcToAttach.PreviousContainerId);
                if (paneContainerToAttach == null)
                    throw new ArgumentException(string.Format("Unable to find a pane with id ='{0}'", lcToAttach.PreviousContainerId));

                lcToAttach.PreviousContainer = paneContainerToAttach as ILayoutContainer;
            }

            //now fix the content of the layoutcontents
            foreach (var lcToFix in layout.Descendents().OfType<LayoutAnchorable>().Where(lc => lc.Content == null).ToArray())
            {
                LayoutAnchorable previousAchorable = null;
                if (lcToFix.ContentId != null)
                {
                    //try find the content in replaced layout
                    previousAchorable = _previousAnchorables.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);
                }

                if (LayoutSerializationCallback != null)
                {
                    var args = new LayoutSerializationCallbackEventArgs(lcToFix, previousAchorable?.Content);
                    LayoutSerializationCallback(this, args);
                    if (args.Cancel)
                        lcToFix.Close();
                    else if (args.Content != null)
                        lcToFix.Content = args.Content;
                    else if (args.Model.Content != null)
                        lcToFix.Hide(false);
                }
                else if (previousAchorable == null)
                    lcToFix.Hide(false);
                else
                {
                    lcToFix.Content = previousAchorable.Content;
                    lcToFix.IconSource = previousAchorable.IconSource;
                }
            }

            foreach (var lcToFix in layout.Descendents().OfType<LayoutDocument>().Where(lc => lc.Content == null).ToArray())
            {
                LayoutDocument previousDocument = null;
                if (lcToFix.ContentId != null)
                {
                    //try find the content in replaced layout
                    previousDocument = _previousDocuments.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);
                }

                if (LayoutSerializationCallback != null)
                {
                    var args = new LayoutSerializationCallbackEventArgs(lcToFix, previousDocument?.Content);
                    LayoutSerializationCallback(this, args);

                    if (args.Cancel)
                        lcToFix.Close();
                    else if (args.Content != null)
                        lcToFix.Content = args.Content;
                    else if (args.Model.Content != null)
                        lcToFix.Close();
                }
                else if (previousDocument == null)
                    lcToFix.Close();
                else
                    lcToFix.Content = previousDocument.Content;
            }

            layout.CollectGarbage();
        }