Exemple #1
0
        internal void AddToContent(XamlObject child)
        {
            PropertyInfo contentProp = ObjType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                       .Single(prop => prop.GetCustomAttribute <ContentPropertyAttribute>() is not null);

            if (contentProp.PropertyType.GetInterface(nameof(IEnumerable)) is null &&
                _children.Count > 1)
            {
                throw new Exception();
            }

            _children.Add(child);
        }
Exemple #2
0
 public ExtendedXamlProperty(string propName, XamlObject extension) :
     base(propName)
 {
     Extension = extension ??
                 throw new ArgumentNullException(nameof(extension));
 }
Exemple #3
0
 public void AddChild(XamlObject child)
 {
     _children.Add(child);
 }
Exemple #4
0
        private XamlObject ReadObject()
        {
            XamlParseContext parentContext  = CurrentContext;
            bool             createNewScope = !_reader.IsEmptyElement;
            string           elementName    = _reader.Name;
            string           ns             = string.Empty;

            if (elementName.Contains(':'))
            {
                var parts = elementName.Split(':', StringSplitOptions.RemoveEmptyEntries);

                ns          = parts[0];
                elementName = parts[1];
            }

            if (elementName.Contains('.'))
            {
                string[] parts = elementName.Split('.', StringSplitOptions.RemoveEmptyEntries);

                if (parts.Length > 2)
                {
                    throw new Exception();
                }

                CurrentContext.CurrentProperty = new FullXamlProperty(parts[1]);
                return(null);
            }

            if (createNewScope)
            {
                EnterScope();
            }

            var attributes = new Dictionary <string, string>();

            if (_reader.HasAttributes)
            {
                _reader.MoveToFirstAttribute();

                do
                {
                    attributes.Add(_reader.Name, _reader.Value);
                }while(_reader.MoveToNextAttribute());

                AddNamespaces(attributes);
            }

            Type objType = CurrentContext.LookupObjectByName(elementName, ns);

            var obj = new XamlObject(objType);

            foreach (var attr in attributes)
            {
                XamlProperty prop;
                string       trimmedValue = attr.Value.Trim();

                if (trimmedValue.StartsWith('{') &&
                    trimmedValue.EndsWith('}'))
                {
                    var parser    = new MarkupExtensionParser(trimmedValue);
                    var extension = parser.ParseExtension(CurrentContext);
                    prop = new ExtendedXamlProperty(attr.Key, extension);
                }
                else if (attr.Key.Contains(':'))
                {
                    var parts = attr.Key.Split(':');

                    if (parts.Length != 2)
                    {
                        throw new XmlException();
                    }

                    prop = new AttachedXamlProperty(parts[1], attr.Value, parts[0]);
                }
                else
                {
                    prop = new InlineXamlProperty(attr.Key, attr.Value);
                }

                obj.AddProperty(prop);
            }

            if (createNewScope)
            {
                CurrentContext.CurrentObject = obj;
            }

            if (parentContext.CurrentProperty is not null &&
                parentContext.CurrentProperty is FullXamlProperty p)
            {
                p.AddChild(obj);
            }