コード例 #1
0
        public static object Create(string xaml, bool doNotThrow = false)
        {
            object inflatedView = null;

            using (var reader = XmlReader.Create(new StringReader(xaml))) {
                while (reader.Read())
                {
                    //Skip until element
                    if (reader.NodeType == XmlNodeType.Whitespace)
                    {
                        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 HydratationContext {
                        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 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);
                        XamlParser.ParseXaml(rootNode, reader);
                        var rNode = (IElementNode)rootNode;
                        if (!rNode.Properties.TryGetValue(new XmlName("http://xamarin.com/schemas/2014/forms", "Resources"), out var resources))
                        {
                            return(null);
                        }

                        var visitorContext = new HydrationContext
                        {
                            ExceptionHandler = ResourceLoader.ExceptionHandler2 != null ? ehandler : (Action <Exception>)null,
                        };
                        var cvv = new CreateValuesVisitor(visitorContext);
                        cvv.Visit((ElementNode)resources, null);
                        visitorContext.RootElement = visitorContext.Values[resources];

                        resources.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);                 //set parents for {StaticResource}
                        resources.Accept(new ExpandMarkupsVisitor(visitorContext), null);
                        resources.Accept(new PruneIgnoredNodesVisitor(false), null);
                        resources.Accept(new NamescopingVisitor(visitorContext), null);                 //set namescopes for {x:Reference}
                        resources.Accept(new CreateValuesVisitor(visitorContext), null);
                        resources.Accept(new RegisterXNamesVisitor(visitorContext), null);
                        resources.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
                        resources.Accept(new ApplyPropertiesVisitor(visitorContext, true), null);

                        return(visitorContext.Values[resources] as IResourceDictionary);
                    }
                }
            return(null);
        }
コード例 #3
0
ファイル: XamlLoader.cs プロジェクト: zkolar/Xamarin.Forms
        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];
                        if (XamlFilePathAttribute.GetFilePathForObject(inflatedView) is string path)
                        {
                            VisualDiagnostics.RegisterSourceInfo(inflatedView, new Uri($"{path};assembly={inflatedView.GetType().GetTypeInfo().Assembly.GetName().Name}", UriKind.Relative), ((IXmlLineInfo)rootnode).LineNumber, ((IXmlLineInfo)rootnode).LinePosition);
                            VisualDiagnostics.SendVisualTreeChanged(null, inflatedView);
                        }
                        visitorContext.RootElement = inflatedView as BindableObject;

                        Visit(rootnode, visitorContext, useDesignProperties);
                        break;
                    }
                }
            return(inflatedView);
        }
コード例 #4
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);
                        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);
                        break;
                    }
                }
            return(inflatedView);
        }
コード例 #5
0
ファイル: XamlLoader.cs プロジェクト: Costo/Xamarin.Forms
		public static object Create (string xaml, bool doNotThrow = false)
		{
			object inflatedView = null;
			using (var reader = XmlReader.Create (new StringReader (xaml))) {
				while (reader.Read ()) {
					//Skip until element
					if (reader.NodeType == XmlNodeType.Whitespace)
						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 HydratationContext {
						DoNotThrowOnExceptions = doNotThrow,
					};
					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;
		}