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); }
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 global::Android.Content.Res.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.XFUri, "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.XFUri || resourcesEN.XmlType.Name != nameof(ResourceDictionary))) //single implicit resource { resources = new ElementNode(new XmlType(XamlParser.XFUri, nameof(ResourceDictionary), null), XamlParser.XFUri, rootNode.NamespaceResolver); ((ElementNode)resources).CollectionItems.Add(resourcesEN); }