Exemplo n.º 1
0
        protected override void LoadInternal(OpenedFile file, System.IO.Stream stream)
        {
            wasChangedInDesigner = false;
            Debug.Assert(file == this.PrimaryFile);
            SD.AnalyticsMonitor.TrackFeature(typeof(WpfViewContent), "Load");

            _stream = new MemoryStream();
            stream.CopyTo(_stream);
            stream.Position = 0;

            if (designer == null)
            {
                // initialize designer on first load
                designer         = new DesignSurface();
                this.UserContent = designer;
                InitPropertyEditor();
                InitWpfToolbox();
            }
            this.UserContent = designer;
            if (outline != null)
            {
                outline.Root = null;
            }


            using (XmlTextReader r = new XmlTextReader(stream)) {
                XamlLoadSettings settings = new XamlLoadSettings();
                settings.DesignerAssemblies.Add(typeof(WpfViewContent).Assembly);
                settings.CustomServiceRegisterFunctions.Add(
                    delegate(XamlDesignContext context) {
                    context.Services.AddService(typeof(IUriContext), new FileUriContext(this.PrimaryFile));
                    context.Services.AddService(typeof(IPropertyDescriptionService), new PropertyDescriptionService(this.PrimaryFile));
                    context.Services.AddService(typeof(IEventHandlerService), new SharpDevelopEventHandlerService(this));
                    context.Services.AddService(typeof(ITopLevelWindowService), new WpfAndWinFormsTopLevelWindowService());
                    context.Services.AddService(typeof(ChooseClassServiceBase), new IdeChooseClassService());
                });
                settings.TypeFinder = MyTypeFinder.Create(this.PrimaryFile);
                try {
                    settings.ReportErrors = UpdateTasks;
                    designer.LoadDesigner(r, settings);

                    designer.DesignPanel.ContextMenuHandler = (contextMenu) => {
                        var newContextmenu     = new ContextMenu();
                        var sdContextMenuItems = MenuService.CreateMenuItems(newContextmenu, designer, "/AddIns/WpfDesign/Designer/ContextMenu", "ContextMenu");
                        foreach (var entry in sdContextMenuItems)
                        {
                            newContextmenu.Items.Add(entry);
                        }
                        newContextmenu.Items.Add(new Separator());

                        var items = contextMenu.Items.Cast <Object>().ToList();
                        contextMenu.Items.Clear();
                        foreach (var entry in items)
                        {
                            newContextmenu.Items.Add(entry);
                        }

                        designer.DesignPanel.ContextMenu = newContextmenu;
                    };

                    if (outline != null && designer.DesignContext != null && designer.DesignContext.RootItem != null)
                    {
                        outline.Root = OutlineNode.Create(designer.DesignContext.RootItem);
                    }

                    propertyGridView.PropertyGrid.SelectedItems = null;
                    designer.DesignContext.Services.Selection.SelectionChanged += OnSelectionChanged;
                    designer.DesignContext.Services.GetService <UndoService>().UndoStackChanged += OnUndoStackChanged;
                } catch (Exception e) {
                    this.UserContent = new WpfDocumentError(e);
                }

                try{
                    var appXaml = SD.ProjectService.CurrentProject.Items.FirstOrDefault(x => x.FileName.GetFileName().ToLower() == ("app.xaml"));
                    if (appXaml != null)
                    {
                        var        f = appXaml as FileProjectItem;
                        OpenedFile a = SD.FileService.GetOrCreateOpenedFile(f.FileName);

                        var xml = XmlReader.Create(a.OpenRead());
                        var doc = new XmlDocument();
                        doc.Load(xml);
                        var node = doc.FirstChild.ChildNodes.Cast <XmlNode>().FirstOrDefault(x => x.Name == "Application.Resources");

                        foreach (XmlAttribute att in doc.FirstChild.Attributes.Cast <XmlAttribute>().ToList())
                        {
                            if (att.Name.StartsWith("xmlns"))
                            {
                                node.Attributes.Append(att);
                            }
                        }

                        var appXamlXml = XmlReader.Create(new StringReader(node.InnerXml));
                        var parsed     = XamlParser.Parse(appXamlXml, ((XamlDesignContext)designer.DesignContext).ParserSettings);
                        var dict       = (ResourceDictionary)parsed.RootInstance;
                        designer.DesignPanel.Resources.MergedDictionaries.Add(dict);
                    }
                } catch (Exception ex) {
                    LoggingService.Error("Error in loading app.xaml", ex);
                }
            }
        }