Exemplo n.º 1
0
        private void _ExtractExpressions(XmlElement element, List <XmlAttribute> expressionAttributes, List <IdViewObject> viewsId)
        {
            if (ParsingHelper.IsResourceTag(element))
            {
                //will be processed later so do not touch it for now
                return;
            }

            string id                    = null;
            bool   isFragment            = ParsingHelper.IsFragmentTag(element);
            List <XmlAttribute> bindings = new List <XmlAttribute>();

            foreach (XmlAttribute attribute in element.Attributes)
            {
                if (ParsingHelper.IsXmlOnlyAttribute(attribute))
                {
                    continue;
                }

                if (ParsingHelper.IsIdAttribute(attribute))
                {
                    if (id != null)
                    {
                        throw new Exception("Multiple id for same element");
                    }

                    id = attribute.Value;
                    // add @+id/ to use auto declare mechanism in Android
                    attribute.Value = "@+id/" + id;

                    //check isFragment cause we will do a findViewById next and it will not works
                    if (!isFragment)
                    {
                        viewsId.Add(new IdViewObject(element.LocalName, id));
                    }
                }
                else if (ParsingHelper.IsCustomAttribute(attribute))
                {
                    bindings.Add(attribute);
                }
                else if (ParsingHelper.IsAttributeWithExpression(attribute))
                {
                    bindings.Add(attribute);
                }
            }

            if (id == null && bindings.Any())
            {
                XmlAttribute attribute = new XmlAttribute();
                id = NameGeneratorHelper.GetViewId();
                attribute.Value        = "@+id/" + id;
                attribute.FullName     = "android:id";
                attribute.NamespaceUri = "http://schemas.android.com/apk/res/android";

                element.Attributes.Add(attribute);

                //check isFragment cause we will do a findViewById next and it will not works
                if (!isFragment)
                {
                    viewsId.Add(new IdViewObject(element.LocalName, id));
                }
            }

            foreach (XmlAttribute attribute in bindings)
            {
                attribute.AttachedId = id;
                element.Attributes.Remove(attribute);
            }
            expressionAttributes.AddRange(bindings);

            foreach (XmlElement child in element.Children)
            {
                _ExtractExpressions(child, expressionAttributes, viewsId);
            }
        }