Ejemplo n.º 1
0
        public static object Create(string xaml, bool doNotThrow, bool useDesignProperties)
        {
            doNotThrow = doNotThrow || ResourceLoader.ExceptionHandler2 != null;
            void ehandler(Exception e) => ResourceLoader.ExceptionHandler2?.Invoke((e, null));

            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 typeArguments = XamlParser.GetTypeArguments(reader);
                        var rootnode      = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, typeArguments), null, (IXmlNamespaceResolver)reader)
                        {
                            LineNumber = ((IXmlLineInfo)reader).LineNumber, LinePosition = ((IXmlLineInfo)reader).LinePosition
                        };

                        XamlParser.ParseXaml(rootnode, reader);
                        var visitorContext = new HydrationContext
                        {
                            ExceptionHandler = doNotThrow ? ehandler : (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, useDesignProperties);
                        VisualDiagnostics.OnChildAdded(null, inflatedView as Element);
                        break;
                    }
                }
            return(inflatedView);
        }
Ejemplo n.º 2
0
        public static IResourceDictionary LoadResources(string xaml, IResourcesProvider rootView)
        {
            void ehandler(Exception e) => ResourceLoader.ExceptionHandler2?.Invoke((e, XamlFilePathAttribute.GetFilePathForObject(rootView)));

            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;
                        }

                        //the root is set to null, and not to rootView, on purpose as we don't want to erase the current Resources of the view
                        RootNode rootNode = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), null, (IXmlNamespaceResolver)reader)
                        {
                            LineNumber = ((IXmlLineInfo)reader).LineNumber, LinePosition = ((IXmlLineInfo)reader).LinePosition
                        };
                        XamlParser.ParseXaml(rootNode, reader);
                        var rNode = (IElementNode)rootNode;
                        if (!rNode.Properties.TryGetValue(new XmlName(XamlParser.MauiUri, "Resources"), out var resources))
                        {
                            return(null);
                        }

                        var visitorContext = new HydrationContext
                        {
                            ExceptionHandler = ResourceLoader.ExceptionHandler2 != null ? ehandler : (Action <Exception>)null,
                        };
                        var cvv = new CreateValuesVisitor(visitorContext);
                        if (resources is ElementNode resourcesEN && (resourcesEN.XmlType.NamespaceUri != XamlParser.MauiUri || resourcesEN.XmlType.Name != nameof(ResourceDictionary)))
                        {                 //single implicit resource
                            resources = new ElementNode(new XmlType(XamlParser.MauiUri, nameof(ResourceDictionary), null), XamlParser.MauiUri, rootNode.NamespaceResolver);
                            ((ElementNode)resources).CollectionItems.Add(resourcesEN);
                        }