public void Visit(ElementNode node, INode parentNode)
        {
            XmlName propertyName;

            //Set ResourcesDictionaries to their parents
            if (IsResourceDictionary(node) && SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
            {
                if ((propertyName.LocalName == "Resources" || propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)))
                {
                    Context.IL.Append(SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node));
                    return;
                }
            }

            //Only proceed further if the node is a keyless RD
            if (parentNode is IElementNode &&
                IsResourceDictionary((IElementNode)parentNode) &&
                !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
            {
                node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
            }
            else if (parentNode is ListNode &&
                     IsResourceDictionary((IElementNode)parentNode.Parent) &&
                     !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
            {
                node.Accept(new SetPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
            }
        }
Exemplo n.º 2
0
        public void Visit(ListNode node, INode parentNode)
        {
            XmlName name;

            if (SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out name))
            {
                node.XmlName = name;
            }
        }
Exemplo n.º 3
0
        public void Visit(ValueNode node, INode parentNode)
        {
            Context.Values[node] = node.Value;

            XmlName propertyName;

            if (SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
            {
                if (propertyName.NamespaceURI == "http://schemas.openxmlformats.org/markup-compatibility/2006" &&
                    propertyName.LocalName == "Ignorable")
                {
                    (parentNode.IgnorablePrefixes ?? (parentNode.IgnorablePrefixes = new List <string>())).AddRange(
                        (node.Value as string).Split(','));
                }
            }
        }
        public void Visit(ValueNode node, INode parentNode)
        {
            XmlName propertyName;

            if (!SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
            {
                if (!IsCollectionItem(node, parentNode))
                {
                    return;
                }
                string contentProperty;
                if (!Context.Variables.ContainsKey((IElementNode)parentNode))
                {
                    return;
                }
                var parentVar = Context.Variables[(IElementNode)parentNode];
                if ((contentProperty = SetPropertiesVisitor.GetContentProperty(parentVar.VariableType)) != null)
                {
                    propertyName = new XmlName(((IElementNode)parentNode).NamespaceURI, contentProperty);
                }
                else
                {
                    return;
                }
            }

            if (node.SkipPrefix((node.NamespaceResolver ?? parentNode.NamespaceResolver)?.LookupPrefix(propertyName.NamespaceURI)))
            {
                return;
            }
            if (propertyName.NamespaceURI == "http://schemas.openxmlformats.org/markup-compatibility/2006" &&
                propertyName.LocalName == "Ignorable")
            {
                (parentNode.IgnorablePrefixes ?? (parentNode.IgnorablePrefixes = new List <string>())).AddRange(
                    (node.Value as string).Split(','));
                return;
            }
            if (propertyName.LocalName != "MergedWith")
            {
                return;
            }
            SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node);
        }
Exemplo n.º 5
0
        public void Visit(ValueNode node, INode parentNode)
        {
            XmlName propertyName;

            if (!SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName))
            {
                if (!IsCollectionItem(node, parentNode))
                {
                    return;
                }
                string contentProperty;
                if (!Context.Variables.ContainsKey((IElementNode)parentNode))
                {
                    return;
                }
                var parentVar = Context.Variables[(IElementNode)parentNode];
                if ((contentProperty = SetPropertiesVisitor.GetContentProperty(parentVar.VariableType)) != null)
                {
                    propertyName = new XmlName(((IElementNode)parentNode).NamespaceURI, contentProperty);
                }
                else
                {
                    return;
                }
            }

            if (propertyName.NamespaceURI == "http://schemas.openxmlformats.org/markup-compatibility/2006" &&
                propertyName.LocalName == "Ignorable")
            {
                return;
            }
            if (propertyName.LocalName != "MergedWith")
            {
                return;
            }
            Context.IL.Append(SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node));
        }
        public void Visit(ElementNode node, INode parentNode)
        {
            //Set Resources in ResourcesDictionaries
            if (IsCollectionItem(node, parentNode) && parentNode is IElementNode)
            {
                // Collection element, implicit content, or implicit collection element.
                CustomAttribute cpAttr;
                var             parentVar = Context.Variables[(IElementNode)parentNode];
                if (parentVar.VariableType.ImplementsInterface(Module.Import(typeof(IEnumerable))))
                {
                    if ((parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" ||
                         parentVar.VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary") &&
                        !node.Properties.ContainsKey(XmlName.xKey))
                    {
                        node.Accept(new SetPropertiesVisitor(Context), parentNode);

                        if (node.XmlType.Name != "Style")
                        {
                            throw new XamlParseException("resources in ResourceDictionary require a x:Key attribute", node);
                        }

                        //if this node is an IMarkupExtension, invoke ProvideValue() and replace the variable
                        var vardef    = Context.Variables[node];
                        var vardefref = new VariableDefinitionReference(vardef);
                        Context.IL.Append(SetPropertiesVisitor.ProvideValue(vardefref, Context, Module, node));
                        if (vardef != vardefref.VariableDefinition)
                        {
                            vardef = vardefref.VariableDefinition;
                            Context.Body.Variables.Add(vardef);
                            Context.Variables[node] = vardef;
                        }

                        Context.IL.Emit(OpCodes.Ldloc, parentVar);
                        Context.IL.Emit(OpCodes.Ldloc, Context.Variables[node]);
                        Context.IL.Emit(OpCodes.Callvirt,
                                        Module.Import(
                                            Module.Import(typeof(ResourceDictionary))
                                            .Resolve()
                                            .Methods.Single(md => md.Name == "Add" && md.Parameters.Count == 1)));
                    }
                    else if ((parentVar.VariableType.FullName == "Xamarin.Forms.ResourceDictionary" ||
                              parentVar.VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary") &&
                             node.Properties.ContainsKey(XmlName.xKey))
                    {
                        node.Accept(new SetPropertiesVisitor(Context), parentNode);

                        //if this node is an IMarkupExtension, invoke ProvideValue() and replace the variable
                        var vardef    = Context.Variables[node];
                        var vardefref = new VariableDefinitionReference(vardef);
                        Context.IL.Append(SetPropertiesVisitor.ProvideValue(vardefref, Context, Module, node));
                        if (vardef != vardefref.VariableDefinition)
                        {
                            vardef = vardefref.VariableDefinition;
                            Context.Body.Variables.Add(vardef);
                            Context.Variables[node] = vardef;
                        }

                        //						IL_0013:  ldloc.0
                        //						IL_0014:  ldstr "key"
                        //						IL_0019:  ldstr "foo"
                        //						IL_001e:  callvirt instance void class [Xamarin.Forms.Core]Xamarin.Forms.ResourceDictionary::Add(string, object)
                        Context.IL.Emit(OpCodes.Ldloc, parentVar);
                        Context.IL.Emit(OpCodes.Ldstr, (node.Properties[XmlName.xKey] as ValueNode).Value as string);
                        var varDef = Context.Variables[node];
                        Context.IL.Emit(OpCodes.Ldloc, varDef);
                        if (varDef.VariableType.IsValueType)
                        {
                            Context.IL.Emit(OpCodes.Box, Module.Import(varDef.VariableType));
                        }
                        Context.IL.Emit(OpCodes.Callvirt,
                                        Module.Import(
                                            Module.Import(typeof(ResourceDictionary))
                                            .Resolve()
                                            .Methods.Single(md => md.Name == "Add" && md.Parameters.Count == 2)));
                    }
                }
            }

            //Set ResourcesDictionaries to their parents
            XmlName propertyName;

            if (SetPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName) &&
                (propertyName.LocalName == "Resources" || propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) &&
                (Context.Variables[node].VariableType.FullName == "Xamarin.Forms.ResourceDictionary" ||
                 Context.Variables[node].VariableType.Resolve().BaseType.FullName == "Xamarin.Forms.ResourceDictionary"))
            {
                SetPropertiesVisitor.SetPropertyValue(Context.Variables[(IElementNode)parentNode], propertyName, node, Context, node);
            }
        }