예제 #1
0
        public static object Create(string xaml, bool doNotThrow = false)
        {
            object inflatedView = null;

            using (var textreader = new StringReader(xaml))
                using (var reader = XmlReader.Create(textreader))
                {
                    while (reader.Read())
                    {
                        //Skip until element
                        if (reader.NodeType == XmlNodeType.Whitespace)
                        {
                            continue;
                        }
                        if (reader.NodeType == XmlNodeType.XmlDeclaration)
                        {
                            continue;
                        }
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value);
                            continue;
                        }

                        var rootnode = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), null, (IXmlNamespaceResolver)reader);
                        XamlParser.ParseXaml(rootnode, reader);
                        var visitorContext = new HydrationContext
                        {
                            ExceptionHandler = doNotThrow ? e => { } : (Action <Exception>)null,
                        };
                        var cvv = new CreateValuesVisitor(visitorContext);
                        cvv.Visit((ElementNode)rootnode, null);
                        inflatedView = rootnode.Root = visitorContext.Values[rootnode];
                        visitorContext.RootElement = inflatedView as BindableObject;

                        Visit(rootnode, visitorContext);
                        break;
                    }
                }
            return(inflatedView);
        }
예제 #2
0
        public static object Create(XmlReader reader, bool doNotThrow = false)
        {
            object inflatedView = null;

            if (reader != null)
            {
                while (reader.Read())
                {
                    //Skip until element
                    if (reader.NodeType == XmlNodeType.Whitespace)
                    {
                        continue;
                    }
                    if (reader.NodeType == XmlNodeType.XmlDeclaration)
                    {
                        continue;
                    }
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value);
                        continue;
                    }

                    var rootnode = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), null, (IXmlNamespaceResolver)reader);
                    XamlParser.ParseXaml(rootnode, reader);
                    var visitorContext = new HydrationContext
                    {
                        ExceptionHandler = doNotThrow ? e => { } : (Action <Exception>)null,
                    };
                    var cvv = new CreateValuesVisitor(visitorContext);

                    // Visit Parameter Properties to create instance from parameterized constructor
                    var type = XamlParser.GetElementType(rootnode.XmlType, rootnode, null, out XamlParseException xpe);
                    if (xpe != null)
                    {
                        throw xpe;
                    }

                    var ctorInfo =
                        type.GetTypeInfo()
                        .DeclaredConstructors.FirstOrDefault(
                            ci =>
                            ci.GetParameters().Length != 0 && ci.IsPublic &&
                            ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof(ParameterAttribute))));
                    if (ctorInfo != null)
                    {
                        foreach (var parameter in ctorInfo.GetParameters())
                        {
                            var propname =
                                parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "Tizen.NUI.Binding.ParameterAttribute")?
                                .ConstructorArguments.First()
                                .Value as string;

                            var name = new XmlName("", propname);
                            if (rootnode.Properties.TryGetValue(name, out INode node) && node is ValueNode)
                            {
                                node.Accept(cvv, rootnode);
                            }
                        }
                    }


                    cvv.Visit((ElementNode)rootnode, null);
                    inflatedView = rootnode.Root = visitorContext.Values[rootnode];
                    visitorContext.RootElement = inflatedView as BindableObject;

                    Visit(rootnode, visitorContext);
                    break;
                }
            }
            return(inflatedView);
        }