Пример #1
0
        private void HandleRootCreation(ref XamlObject obj, XamlParseContext context)
        {
            int nameStart = _current;

            while ((char.IsLetter(CurrentChar) || CurrentChar == ':') && MoveNext())
            {
            }

            string typeName = _value.Substring(nameStart, _current - nameStart);
            string ns       = string.Empty;

            if (typeName.Contains(':'))
            {
                var parts = typeName.Split(':');

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

            Type extensionType = context.LookupObjectByName(typeName, ns);

            if (extensionType is null)
            {
                throw new Exception();
            }

            obj = new XamlObject(extensionType);
        }
Пример #2
0
        public XamlObject ParseExtension(XamlParseContext context)
        {
            _current = -1;

            XamlObject obj       = default;
            string     attrName  = string.Empty;
            string     attrValue = string.Empty;

            while (MoveNext())
            {
                if (CurrentChar == '{')
                {
                    MoveNext();

                    while (char.IsWhiteSpace(CurrentChar))
                    {
                        MoveNext();
                    }

                    HandleRootCreation(ref obj, context);
                }
                else if (CurrentChar == '}')
                {
                    return(obj);
                }
                else if (char.IsWhiteSpace(CurrentChar) ||
                         !string.IsNullOrEmpty(attrName) && CurrentChar == '=')
                {
                    continue;
                }
                else if (string.IsNullOrEmpty(attrName))
                {
                    attrName = ReadAttributeName();
                    _current--;
                }
                else if (!string.IsNullOrEmpty(attrName) &&
                         string.IsNullOrEmpty(attrValue))
                {
                    if (CurrentChar == '{')
                    {
                        var        innerParser    = new MarkupExtensionParser(_value.Substring(_current));
                        XamlObject innerExtension = innerParser.ParseExtension(context);
                        obj.AddProperty(new ExtendedXamlProperty(attrName, innerExtension));

                        _current += innerParser._current;
                    }
                    else
                    {
                        attrValue = ReadValue();
                        obj.AddProperty(new InlineXamlProperty(attrName, attrValue));
                        _current--;
                    }

                    attrName = attrValue = string.Empty;
                }
            }

            return(obj);
        }
Пример #3
0
        public XamlReader(TextReader input)
        {
            _reader = new XmlTextReader(input);
            _contexts.Push(XamlParseContext.CreateRootContext());

            //Add option to modify it by user
            CurrentContext.AddNamespace(new XamlNamespace(string.Empty, "DotX.Styling", "DotX"));
            CurrentContext.AddNamespace(new XamlNamespace(string.Empty, "DotX.Xaml.MarkupExtensions", "DotX.Xaml"));
            CurrentContext.AddNamespace(new XamlNamespace(string.Empty, "DotX.Brush", "DotX"));
        }
Пример #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);
            }
Пример #5
0
 public XamlParseContext(XamlParseContext parentContext)
 {
     _parent = parentContext;
 }