GetService() public method

public GetService ( Type serviceType ) : object
serviceType System.Type
return object
Beispiel #1
0
        /// <summary>
        /// Gets the designer surface init parameters
        /// For now they're used for preserving selected items between reloads
        /// </summary>
        void GetDesignerInitParams(StringBuilder strBuilder)
        {
            List <string> clientIds = new List <string> (host.Container.Components.Count);

            foreach (IComponent comp in host.Container.Components)
            {
                clientIds.Add((comp as Control).ClientID);
            }

            var selServ = host.GetService(typeof(ISelectionService)) as ISelectionService;

            if (selServ == null)
            {
                throw new Exception("Could not load selection service");
            }
            ICollection   col         = selServ.GetSelectedComponents();
            List <string> selectedIds = new List <string> (col.Count);

            foreach (IComponent comp in col)
            {
                selectedIds.Add((comp as Control).ClientID);
            }

            System.Web.Script.Serialization.JavaScriptSerializer jsonizer = new System.Web.Script.Serialization.JavaScriptSerializer();
            strBuilder.AppendFormat("<div id=\"{0}\" style=\"display:none;\"> ", DesignerNames.ElementInitContClass);
            strBuilder.AppendFormat("<span id=\"{0}\">", DesignerNames.ElementSelectableContClass);
            jsonizer.Serialize(clientIds.ToArray(), strBuilder);
            strBuilder.Append("</span>");
            strBuilder.AppendFormat("<span id=\"{0}\">", DesignerNames.ElementSeletectedContClass);
            jsonizer.Serialize(selectedIds.ToArray(), strBuilder);
            strBuilder.Append("</span>");
            strBuilder.Append("</div>");
        }
Beispiel #2
0
 //TODO: Some way to reset this when host is reset
 public WebFormReferenceManager(DesignerHost host)
 {
     if (host == null)
     {
         throw new ArgumentNullException("host");
     }
     this.host    = host;
     this.typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService;
     if (typeRes == null)
     {
         throw new Exception("Could not obtain ITypesResolutionService from host");
     }
 }
Beispiel #3
0
        public void Add(IComponent component, string name)
        {
            IDesigner designer = null;

            //input checks
            if (component == null)
            {
                throw new ArgumentException("Cannot add null component to container", "component");
            }
            if (!(component is Control))
            {
                throw new ArgumentException("This Container only accepts System.Web.UI.Control-derived components", "component");
            }
            if (component.Site != null && component.Site.Container != this)
            {
                component.Site.Container.Remove(component);
            }

            //Check the name and create one if necessary
            INameCreationService nameService = host.GetService(typeof(INameCreationService)) as INameCreationService;

            if (nameService == null)
            {
                throw new Exception("The container must have access to a INameCreationService implementation");
            }

            if (name == null || !nameService.IsValidName(name))
            {
                name = nameService.CreateName(this, component.GetType());
                System.Diagnostics.Trace.WriteLine("Generated name for component: " + name);
            }

            //check we don't already have component with same name
            if (GetComponent(name) != null)
            {
                throw new ArgumentException("There is already a component with this name in the container", "name");
            }

            //we're definately adding it now, so broadcast
            OnComponentAdding(component);

            //get a site and set ID property
            //this way (not PropertyDescriptor.SetValue) won't fire change events
            ((Control)component).ID = name;
            component.Site          = new DesignSite(component, this);

            //Get designer. If first component, designer must be an IRootDesigner
            if (components.Count == 0)
            {
                host.SetRootComponent(component);
                designer = new RootDesigner(component);
            }

            //FIXME: Give Mono some base designers to find! We should never encounter this!
            //else
            //	designer = TypeDescriptor.CreateDesigner (component, typeof(System.ComponentModel.Design.IDesigner));


            if (designer == null)
            {
                //component.Site = null;
                //throw new Exception ("Designer could not be obtained for this component.");
            }
            else
            {
                //track and initialise it
                designers.Add(component, designer);
                designer.Initialize(component);
            }

            //add references to referenceManager, unless root component
            if (components.Count != 1)
            {
                host.WebFormReferenceManager.AddReference(component.GetType());
            }

            //Finally put in container
            components.Add(component);

            //and broadcast completion
            OnComponentAdded(component);
        }
Beispiel #4
0
        /// <summary>
        /// Checks the document for control tags, creates components and adds the to the IContainer.
        /// Adds an id attributes to tags that are server controls but doesn't have an id attribute.
        /// </summary>
        void ParseControls()
        {
            // no need to serialize the document, if we add just an id attribute to a control
            suppressSerialization = true;

            // if an id tag was added the document changes
            // so we parse the document each time it does
            do
            {
                // get a fresh new AspNetParsedDocument
                var doc = Parse();

                // go through all the nodes of the document
                foreach (XNode node in doc.XDocument.RootElement.AllDescendentElements)
                {
                    // if a node is not a XElement, no need to check if it's a control
                    if (!(node is XElement))
                    {
                        continue;
                    }

                    var element = node as XElement;

                    // the controls have a tag prefix or runat="server" attribute
                    if (element.Name.HasPrefix || XDocumentHelper.IsRunAtServer(element))
                    {
                        string id = XDocumentHelper.GetAttributeValueCI(element.Attributes, "id");

                        // check the DesignContainer if a component for that node already exists
                        if (host.GetComponent(id) == null)
                        {
                            // create a component of type depending of the element
                            IComponent comp = ProcessControl(element);

                            if (comp == null)
                            {
                                continue;
                            }

                            // add id to the component, for later recognition if it has no ID attribute
                            if (String.IsNullOrEmpty(id))
                            {
                                var nameServ = host.GetService(typeof(INameCreationService)) as INameCreationService;
                                if (nameServ == null)
                                {
                                    throw new Exception("Could not obtain INameCreationService from the DesignerHost.");
                                }

                                // insert the attribute to the element
                                host.AspNetSerializer.SetAttribtue(element, "id", nameServ.CreateName(host.Container, comp.GetType()));
                                updateEditorContent.WaitOne();                                  // wait until the changes have been applied to the document
                                break;
                            }

                            // we have a control component, add it to the container
                            this.host.Container.Add(comp, id);
                            // and parse its attributes for component properties
                            ProcessControlProperties(element, comp);
                        }
                    }
                }
            } while (txtDocDirty);

            suppressSerialization = false;
        }