Пример #1
0
 private void OnOpenDocumentCommandExecute(IPackageContent document)
 {
     if (document == null)
     {
         return;
     }
     if (document is DocumentUnopen)
     {
         var docUnopen = document as DocumentUnopen;
         if (docUnopen.KeepAliveWhenClose)
         {
             var documentViewMoel = (IDocumentViewModel)docUnopen.DocClosedButAlive;
             ShellService.OpenDocument(documentViewMoel);
             ShellService.ActivateDocument(documentViewMoel);
             ShellService.DocumentsUnopen.Remove(docUnopen);
         }
         else
         {
             using (Package package = Package.Open(CurrentPackagePath, FileMode.Open))
             {
                 var documentViewMoel = OpenPackageContent(package, docUnopen.ContentId) as DocumentViewModel;
                 ShellService.OpenDocument(documentViewMoel);
                 ShellService.ActivateDocument(documentViewMoel);
                 var shellService = (ShellService as ShellService);
                 ShellService.DocumentsUnopen.Remove(docUnopen);
                 SaveDocumentsUnopen(package, ShellService.DocumentsUnopen);
                 SavePanelLayout(package);
             }
         }
     }
     else
     {
         ShellService.ActivateDocument(document as IDocumentViewModel);
     }
 }
Пример #2
0
        void OpenPackage(string packageFilePath)
        {
            if (!File.Exists(packageFilePath))
            {
                Log.Error("File Not Exist!");
                return;
            }
            _oderOpenDocumentItemList.Clear();
            int oderOpenDocumentItemNum = 0;
            var shellService            = ((ShellService)(ShellService));

            using (Package package = Package.Open(packageFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                //_EventAggregator.GetEvent<PackageBeforeOpenEvent>().Publish(package);
                PackageBeforeOpenEvent.SendWith(package);
                //load documents unopen
                var packageRelationShipDocumentsUnopen = package.GetRelationship(Package_DocumentsUnopen_ID);
                var layoutPartDocumentsUnopen          = package.GetPart(packageRelationShipDocumentsUnopen.TargetUri);
                var xmlSerilalizer = new System.Xml.Serialization.XmlSerializer(typeof(ObservableCollection <DocumentUnopen>));
                var unopenDocs     = xmlSerilalizer.Deserialize(layoutPartDocumentsUnopen.GetStream()) as ObservableCollection <DocumentUnopen>;
                if (unopenDocs != null)
                {
                    foreach (var item in unopenDocs)
                    {
                        shellService.DocumentsUnopen.Add(item);
                        if (item.KeepAliveWhenClose)
                        {
                            _oderOpenDocumentItemList.Add(new OderOpenDocumentItem()
                            {
                                IsUnopenDoc        = true,
                                UnopenDocContentId = item.ContentId,
                                DocUnopen          = item
                            });
                        }
                    }
                }
                //load avalondock panel layout
                var layoutRelationShip = package.GetRelationship(Package_PanelLayoutPart_ID);
                var layoutPart         = package.GetPart(layoutRelationShip.TargetUri);
                var packageManger      = this.GetDependencyResolver().Resolve <DockingManager>();
                var layoutSerializer   = new XmlLayoutSerializer(packageManger);
                layoutSerializer.LayoutSerializationCallback += (s, e) =>
                {
                    //Get LayoutContentViewModel From Package
                    if (e.Model is LayoutAnchorable)
                    {
                        var layoutAnchorable = e.Model as LayoutAnchorable;
                        Debug.Assert(package != null, "package != null");
                        var contentViewModel = OpenPackageContent(package, e.Model.ContentId);
                        var toolViewModel    = contentViewModel as IToolViewModel;
                        System.Diagnostics.Debug.Assert(toolViewModel != null);
                        e.Content = contentViewModel;
                        if (!ShellService.Tools.Contains_CompareByReference(toolViewModel))
                        {
                            ShellService.Tools.Add(toolViewModel);
                        }
                        toolViewModel.IsSelected = layoutAnchorable.IsSelected;
                        toolViewModel.IsActive   = layoutAnchorable.IsActive;
                        toolViewModel.IsVisible  = layoutAnchorable.IsVisible;
                        System.Diagnostics.Debug.Assert(ShellService.Documents.Count == 0);//the code of the avalondock behave in this kind of behavior, and the whole application assume the tools are loaded first.
                        return;
                    }
                    else if (e.Model is LayoutDocument)
                    {
                        var layoutDocument = e.Model as LayoutDocument;

                        //var contentViewModel = Services.ShellService.OpenPackageContent(package, e.Model.ContentId) as IDocumentViewModel;
                        //e.Content = contentViewModel;
                        //if (!ShellService.Documents.Contains_CompareByReference(contentViewModel))
                        //{
                        //    ShellService.Documents.Add(contentViewModel);
                        //}
                        //contentViewModel.IsSelected = layoutDocument.IsSelected;
                        //contentViewModel.IsActive = layoutDocument.IsActive;

                        _oderOpenDocumentItemList.Add(new OderOpenDocumentItem()
                        {
                            Num = oderOpenDocumentItemNum, LayoutContent = layoutDocument
                        });
                        oderOpenDocumentItemNum++;
                        e.Cancel = true;
                        return;
                    }
                    else
                    {
                        return;
                    }
                };

                layoutSerializer.Deserialize(layoutPart.GetStream());
                foreach (var item in _oderOpenDocumentItemList)
                {
                    if (!item.IsUnopenDoc)
                    {
                        //bug fix for avalondock start form 2.1.0, because the close() methord work like 2.0.0
                        //in 2.0.0 close(){
                        //ILayoutRoot root = base.Root;
                        //ILayoutContainer parentAsContainer = base.Parent;
                        //parentAsContainer.RemoveChild(this);
                        //if (root != null)
                        //{
                        //    root.CollectGarbage();
                        //}}
                        //if (item.LayoutContent.Parent != null)
                        //{
                        item.LayoutContent.Parent.RemoveChild(item.LayoutContent);
                        //}
                    }
                }
                //Order Open Document;
                _oderOpenDocumentItemList.Sort((item) =>
                {
                    string packagePartId;
                    string contentId     = null;
                    contentId            = item.IsUnopenDoc ? item.UnopenDocContentId : item.LayoutContent.ContentId;
                    Type packagePartType = ContentID_ToTypeAndPackagePartID(contentId, out packagePartId);
                    return(packagePartType.GetAttributeValue((DocumentOpenOrderAttribute a) => a.Value));
                });

                IDocumentViewModel lastActiveDocument = null;
                foreach (var doc in _oderOpenDocumentItemList)
                {
                    if (doc.IsUnopenDoc)
                    {
                        var contentViewModel  = OpenPackageContent(package, doc.UnopenDocContentId);
                        var documentViewModel = contentViewModel as IDocumentViewModel;
                        System.Diagnostics.Debug.Assert(documentViewModel != null);
                        doc.DocUnopen.DocClosedButAlive = documentViewModel;
                        doc.DocUnopen = null;
                    }
                    else
                    {
                        var contentViewModel  = OpenPackageContent(package, doc.LayoutContent.ContentId);
                        var documentViewModel = contentViewModel as IDocumentViewModel;
                        System.Diagnostics.Debug.Assert(documentViewModel != null);
                        doc.LayoutContentViewModel = documentViewModel;
                        ShellService.OpenDocument(documentViewModel);
                        if (doc.LayoutContent.IsLastFocusedDocument)
                        {
                            lastActiveDocument = doc.LayoutContentViewModel;
                        }
                    }
                }
                _oderOpenDocumentItemList.RemoveAll(x => x.IsUnopenDoc);
                _oderOpenDocumentItemList.Sort((item) => item.Num);

                for (int i = 0; i < _oderOpenDocumentItemList.Count; i++)
                {
                    int indexInShellDocuments = ShellService.Documents.IndexOf(_oderOpenDocumentItemList[i].LayoutContentViewModel);
                    if (i != indexInShellDocuments)
                    {
                        ShellService.Documents.Move(indexInShellDocuments, i);
                    }
                    _oderOpenDocumentItemList[i].LayoutContentViewModel = null;
                }
                if (lastActiveDocument != null)
                {
                    ShellService.ActivateDocument(lastActiveDocument);
                }
                PackageAfterOpenEvent.SendWith(package);
                _oderOpenDocumentItemList.Clear();
            }
        }