Example #1
0
        /// <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);
			}
			
			_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);
		}