protected override void LoadInternal(OpenedFile file, System.IO.Stream stream) { Debug.Assert(file == this.PrimaryFile); _stream = new MemoryStream(); stream.CopyTo(_stream); stream.Position = 0; if (designer == null) { // initialize designer on first load designer = new DesignSurface(); this.UserContent = designer; InitPropertyEditor(); } 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 CSharpEventHandlerService(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.ContextMenuOpening += (sender, e) => MenuService.ShowContextMenu(e.OriginalSource as UIElement, designer, "/AddIns/WpfDesign/Designer/ContextMenu"); 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 { this.UserContent=new WpfDocumentError(); } } }
void UpdateDesign() { OutlineRoot = null; using (var xmlReader = XmlReader.Create(new StringReader(Text))) { XamlLoadSettings settings = new XamlLoadSettings(); foreach (var assNode in Toolbox.Instance.AssemblyNodes) { settings.DesignerAssemblies.Add(assNode.Assembly); } settings.TypeFinder = MyTypeFinder.Instance; DesignSurface.LoadDesigner(xmlReader, settings); } if (DesignContext.RootItem != null) { OutlineRoot = OutlineNode.Create(DesignContext.RootItem); UndoService.UndoStackChanged += new EventHandler(UndoService_UndoStackChanged); } RaisePropertyChanged("SelectionService"); RaisePropertyChanged("XamlErrorService"); }
/// <summary> /// Creates a new XamlDesignContext instance. /// </summary> public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings) { if (xamlReader == null) throw new ArgumentNullException("xamlReader"); if (loadSettings == null) throw new ArgumentNullException("loadSettings"); this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService()); this.Services.AddService(typeof(IToolService), new DefaultToolService(this)); this.Services.AddService(typeof(UndoService), new UndoService()); this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this)); this.Services.AddService(typeof(ViewService), new DefaultViewService(this)); this.Services.AddService(typeof(OptionService), new OptionService()); var xamlErrorService = new XamlErrorService(); this.Services.AddService(typeof(XamlErrorService), xamlErrorService); this.Services.AddService(typeof(IXamlErrorSink), xamlErrorService); _componentService = new XamlComponentService(this); this.Services.AddService(typeof(IComponentService), _componentService); foreach (Action<XamlDesignContext> action in loadSettings.CustomServiceRegisterFunctions) { action(this); } // register default versions of overridable services: if (this.Services.GetService(typeof(ITopLevelWindowService)) == null) { this.Services.AddService(typeof(ITopLevelWindowService), new WpfTopLevelWindowService()); } // register extensions from the designer assemblies: foreach (Assembly designerAssembly in loadSettings.DesignerAssemblies) { this.Services.ExtensionManager.RegisterAssembly(designerAssembly); EditorManager.RegisterAssembly(designerAssembly); } _parserSettings = new XamlParserSettings(); _parserSettings.TypeFinder = loadSettings.TypeFinder; _parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory; _parserSettings.ServiceProvider = this.Services; _doc = XamlParser.Parse(xamlReader, _parserSettings); loadSettings.ReportErrors(xamlErrorService); if (_doc == null) { string message; if (xamlErrorService != null && xamlErrorService.Errors.Count > 0) message = xamlErrorService.Errors[0].Message; else message = "Could not load document."; throw new XamlLoadException(message); } _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement); if(_rootItem!=null){ var rootBehavior=new RootItemBehavior(); rootBehavior.Intialize(this); } _xamlEditOperations=new XamlEditOperations(this,_parserSettings); }
/// <summary> /// Creates a new XamlDesignContext instance. /// </summary> public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings) { if (xamlReader == null) { throw new ArgumentNullException("xamlReader"); } if (loadSettings == null) { throw new ArgumentNullException("loadSettings"); } this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService()); this.Services.AddService(typeof(IToolService), new DefaultToolService(this)); this.Services.AddService(typeof(UndoService), new UndoService()); this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this)); this.Services.AddService(typeof(ViewService), new DefaultViewService(this)); this.Services.AddService(typeof(OptionService), new OptionService()); var xamlErrorService = new XamlErrorService(); this.Services.AddService(typeof(XamlErrorService), xamlErrorService); this.Services.AddService(typeof(IXamlErrorSink), xamlErrorService); _componentService = new XamlComponentService(this); this.Services.AddService(typeof(IComponentService), _componentService); foreach (Action <XamlDesignContext> action in loadSettings.CustomServiceRegisterFunctions) { action(this); } // register default versions of overridable services: if (this.Services.GetService(typeof(ITopLevelWindowService)) == null) { this.Services.AddService(typeof(ITopLevelWindowService), new WpfTopLevelWindowService()); } // register extensions from the designer assemblies: foreach (Assembly designerAssembly in loadSettings.DesignerAssemblies) { this.Services.ExtensionManager.RegisterAssembly(designerAssembly); EditorManager.RegisterAssembly(designerAssembly); } XamlParserSettings parserSettings = new XamlParserSettings(); parserSettings.TypeFinder = loadSettings.TypeFinder; parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory; parserSettings.ServiceProvider = this.Services; _doc = XamlParser.Parse(xamlReader, parserSettings); if (_doc == null) { loadSettings.ReportErrors(xamlErrorService); } _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement); if (_rootItem != null) { var rootBehavior = new RootItemBehavior(); rootBehavior.Intialize(this); } _xamlEditOperations = new XamlEditOperations(this, parserSettings); }
/// <summary> /// Creates a new XamlDesignContext instance. /// </summary> public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings) { if (xamlReader == null) { throw new ArgumentNullException("xamlReader"); } if (loadSettings == null) { throw new ArgumentNullException("loadSettings"); } this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService()); this.Services.AddService(typeof(IComponentPropertyService), new ComponentPropertyService()); this.Services.AddService(typeof(IToolService), new DefaultToolService(this)); this.Services.AddService(typeof(UndoService), new UndoService()); this.Services.AddService(typeof(ICopyPasteService), new CopyPasteService()); this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this)); this.Services.AddService(typeof(IOutlineNodeNameService), new OutlineNodeNameService()); this.Services.AddService(typeof(ViewService), new DefaultViewService(this)); this.Services.AddService(typeof(OptionService), new OptionService()); var xamlErrorService = new XamlErrorService(); this.Services.AddService(typeof(XamlErrorService), xamlErrorService); this.Services.AddService(typeof(IXamlErrorSink), xamlErrorService); _componentService = new XamlComponentService(this); this.Services.AddService(typeof(IComponentService), _componentService); foreach (Action <XamlDesignContext> action in loadSettings.CustomServiceRegisterFunctions) { action(this); } // register default versions of overridable services: if (this.Services.GetService(typeof(ITopLevelWindowService)) == null) { this.Services.AddService(typeof(ITopLevelWindowService), new WpfTopLevelWindowService()); } EditorManager.SetDefaultTextBoxEditorType(typeof(TextBoxEditor)); EditorManager.SetDefaultComboBoxEditorType(typeof(ComboBoxEditor)); // register extensions from the designer assemblies: foreach (Assembly designerAssembly in loadSettings.DesignerAssemblies) { this.Services.ExtensionManager.RegisterAssembly(designerAssembly); EditorManager.RegisterAssembly(designerAssembly); } _parserSettings = new XamlParserSettings(); _parserSettings.TypeFinder = loadSettings.TypeFinder; _parserSettings.CurrentProjectAssemblyName = loadSettings.CurrentProjectAssemblyName; _parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory; _parserSettings.ServiceProvider = this.Services; _doc = XamlParser.Parse(xamlReader, _parserSettings); loadSettings.ReportErrors(xamlErrorService); if (_doc == null) { string message; if (xamlErrorService != null && xamlErrorService.Errors.Count > 0) { message = xamlErrorService.Errors[0].Message; } else { message = "Could not load document."; } throw new XamlLoadException(message); } _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement); if (_rootItem != null) { var rootBehavior = new RootItemBehavior(); rootBehavior.Intialize(this); } _xamlEditOperations = new XamlEditOperations(this, _parserSettings); }
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); settings.CurrentProjectAssemblyName = SD.ProjectService.CurrentProject.AssemblyName; try { if (WpfEditorOptions.EnableAppXamlParsing) { 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")) { foreach (var childNode in node.ChildNodes.OfType<XmlNode>()) { childNode.Attributes.Append(att); } } } var appXamlXml = XmlReader.Create(new StringReader(node.InnerXml)); var appxamlContext = new XamlDesignContext(appXamlXml, settings); //var parsed = XamlParser.Parse(appXamlXml, appxamlContext.ParserSettings); var dict = (ResourceDictionary) appxamlContext.RootItem.Component;// parsed.RootInstance; designer.DesignPanel.Resources.MergedDictionaries.Add(dict); } } } catch (Exception ex) { LoggingService.Error("Error in loading app.xaml", ex); } 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); } } }