예제 #1
0
 public bool IsResourceDictionary(ElementNode node) => typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]);
예제 #2
0
        public void Visit(ElementNode node, INode parentNode)
        {
            object value = null;

            var type = XamlParser.GetElementType(node.XmlType, node, Context.RootElement?.GetType().GetTypeInfo().Assembly,
                                                 out XamlParseException xpe);

            if (xpe != null)
            {
                if (Context.ExceptionHandler != null)
                {
                    Context.ExceptionHandler(xpe);
                    return;
                }
                throw xpe;
            }
            Context.Types[node] = type;
            if (IsXaml2009LanguagePrimitive(node))
            {
                value = CreateLanguagePrimitive(type, node);
            }
            else if (node.Properties.ContainsKey(XmlName.xArguments) || node.Properties.ContainsKey(XmlName.xFactoryMethod))
            {
                value = CreateFromFactory(type, node);
            }
            else if (
                type.GetTypeInfo()
                .DeclaredConstructors.Any(
                    ci =>
                    ci.IsPublic && ci.GetParameters().Length != 0 &&
                    ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof(ParameterAttribute)))) &&
                ValidateCtorArguments(type, node, out string ctorargname))
            {
                value = CreateFromParameterizedConstructor(type, node);
            }
            else if (!type.GetTypeInfo().DeclaredConstructors.Any(ci => ci.IsPublic && ci.GetParameters().Length == 0) &&
                     !ValidateCtorArguments(type, node, out ctorargname))
            {
                throw new XamlParseException($"The Property {ctorargname} is required to create a {type.FullName} object.", node);
            }
            else
            {
                //this is a trick as the DataTemplate parameterless ctor is internal, and we can't CreateInstance(..., false) on WP7
                try
                {
                    if (type == typeof(DataTemplate))
                    {
                        value = new DataTemplate();
                    }
                    if (type == typeof(ControlTemplate))
                    {
                        value = new ControlTemplate();
                    }
                    if (value == null && node.CollectionItems.Any() && node.CollectionItems.First() is ValueNode)
                    {
                        var serviceProvider = new XamlServiceProvider(node, Context);
                        var converted       = ((ValueNode)node.CollectionItems.First()).Value.ConvertTo(type, () => type.GetTypeInfo(),
                                                                                                        serviceProvider, out Exception exception);
                        if (exception != null)
                        {
                            if (Context.ExceptionHandler != null)
                            {
                                Context.ExceptionHandler(exception);
                                return;
                            }
                            throw exception;
                        }
                        if (converted != null && converted.GetType() == type)
                        {
                            value = converted;
                        }
                    }
                    if (value == null)
                    {
                        value = Activator.CreateInstance(type);
                    }
                }
                catch (TargetInvocationException e) when(e.InnerException is XamlParseException || e.InnerException is XmlException)
                {
                    throw e.InnerException;
                }
                catch (MissingMemberException mme)
                {
                    throw new XamlParseException(mme.Message, node, mme);
                }
            }

            Values[node] = value;

            if (value is IMarkupExtension markup && (value is TypeExtension || value is StaticExtension || value is ArrayExtension))
            {
                var serviceProvider = new XamlServiceProvider(node, Context);

                var visitor = new ApplyPropertiesVisitor(Context);
                foreach (var cnode in node.Properties.Values.ToList())
                {
                    cnode.Accept(visitor, node);
                }
                foreach (var cnode in node.CollectionItems)
                {
                    cnode.Accept(visitor, node);
                }

                try
                {
                    value = markup.ProvideValue(serviceProvider);
                }
                catch (Exception e)
                {
                    var xamlpe = e as XamlParseException ?? new XamlParseException("Markup extension failed", serviceProvider, e);
                    if (Context.ExceptionHandler != null)
                    {
                        Context.ExceptionHandler(xamlpe);
                    }
                    else
                    {
                        throw xamlpe;
                    }
                }
                if (!node.Properties.TryGetValue(XmlName.xKey, out INode xKey))
                {
                    xKey = null;
                }

                node.Properties.Clear();
                node.CollectionItems.Clear();

                if (xKey != null)
                {
                    node.Properties.Add(XmlName.xKey, xKey);
                }

                Values[node] = value;
            }

            if (value is BindableObject bindableValue && node.NameScopeRef != (parentNode as IElementNode)?.NameScopeRef)
            {
                NameScope.SetNameScope(bindableValue, node.NameScopeRef.NameScope);
            }

            var assemblyName = (Context.RootAssembly ?? Context.RootElement?.GetType().GetTypeInfo().Assembly)?.GetName().Name;

            if (assemblyName != null && value != null && !value.GetType().GetTypeInfo().IsValueType&& XamlFilePathAttribute.GetFilePathForObject(Context.RootElement) is string path)
            {
                Diagnostics.VisualDiagnostics.RegisterSourceInfo(value, new Uri($"{path};assembly={assemblyName}", UriKind.Relative), ((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition);
            }
        }
예제 #3
0
파일: XamlParser.cs 프로젝트: sung-su/maui
        static INode ReadNode(XmlReader reader, bool nested = false)
        {
            var skipFirstRead = nested;

            Debug.Assert(reader.NodeType == XmlNodeType.Element);
            var name  = reader.Name;
            var nodes = new List <INode>();

            while (skipFirstRead || reader.Read())
            {
                skipFirstRead = false;

                INode node;
                switch (reader.NodeType)
                {
                case XmlNodeType.EndElement:
                    Debug.Assert(reader.Name == name);
                    if (nodes.Count == 0)                             //Empty element
                    {
                        return(null);
                    }
                    if (nodes.Count == 1)
                    {
                        return(nodes[0]);
                    }
                    return(new ListNode(nodes, (IXmlNamespaceResolver)reader, ((IXmlLineInfo)reader).LineNumber,
                                        ((IXmlLineInfo)reader).LinePosition));

                case XmlNodeType.Element:
                    var isEmpty        = reader.IsEmptyElement && reader.Name == name;
                    var elementName    = reader.Name;
                    var elementNsUri   = reader.NamespaceURI;
                    var elementXmlInfo = (IXmlLineInfo)reader;
                    IList <KeyValuePair <string, string> > xmlns;

                    var attributes    = ParseXamlAttributes(reader, out xmlns);
                    var prefixes      = PrefixesToIgnore(xmlns);
                    var typeArguments = GetTypeArguments(attributes);

                    node = new ElementNode(new XmlType(elementNsUri, elementName, typeArguments), elementNsUri,
                                           reader as IXmlNamespaceResolver, elementXmlInfo.LineNumber, elementXmlInfo.LinePosition);
                    ((IElementNode)node).Properties.AddRange(attributes);
                    (node.IgnorablePrefixes ?? (node.IgnorablePrefixes = new List <string>())).AddRange(prefixes);

                    ParseXamlElementFor((IElementNode)node, reader);
                    nodes.Add(node);
                    if (isEmpty || nested)
                    {
                        return(node);
                    }
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    node = new ValueNode(reader.Value.Trim(), (IXmlNamespaceResolver)reader, ((IXmlLineInfo)reader).LineNumber,
                                         ((IXmlLineInfo)reader).LinePosition);
                    nodes.Add(node);
                    break;

                case XmlNodeType.Whitespace:
                    break;

                default:
                    Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value);
                    break;
                }
            }
            throw new XamlParseException("Closing PropertyElement expected", (IXmlLineInfo)reader);
        }
예제 #4
0
        public void Visit(ElementNode node, INode parentNode)
        {
            if (node.XmlType.Name != nameof(OnPlatformExtension) || node.XmlType.NamespaceUri != XamlParser.MauiUri)
            {
                return;
            }
            if (string.IsNullOrEmpty(TargetFramework))
            {
                return;
            }

            string target = null;

#if NETSTANDARD2_0
            if (TargetFramework.Contains("-android"))
#else
            if (TargetFramework.Contains("-android", StringComparison.Ordinal))
#endif
            { target = nameof(OnPlatformExtension.Android); }
#if NETSTANDARD2_0
            if (TargetFramework.Contains("-ios"))
#else
            if (TargetFramework.Contains("-ios", StringComparison.Ordinal))
#endif
            { target = nameof(OnPlatformExtension.iOS); }
#if NETSTANDARD2_0
            if (TargetFramework.Contains("-macos"))
#else
            if (TargetFramework.Contains("-macos", StringComparison.Ordinal))
#endif
            { target = nameof(OnPlatformExtension.macOS); }
#if NETSTANDARD2_0
            if (TargetFramework.Contains("-maccatalyst"))
#else
            if (TargetFramework.Contains("-maccatalyst", StringComparison.Ordinal))
#endif
            { target = nameof(OnPlatformExtension.MacCatalyst); }

            if (target is null)
            {
                return;
            }

            if (node.Properties.TryGetValue(new XmlName("", target), out INode targetNode) ||
                node.Properties.TryGetValue(new XmlName("", nameof(OnPlatformExtension.Default)), out targetNode))
            {
                if (!ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name))
                {
                    return;
                }
                if (parentNode is IElementNode parentEnode)
                {
                    parentEnode.Properties[name] = targetNode;
                }
            }
            else             //no prop for target and no Default set
            {
                if (!ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name))
                {
                    return;
                }
                //if there's no value for the targetPlatform, ignore the node.
                //this is slightly different than what OnPlatform does (return default(T))
                if (parentNode is IElementNode parentEnode)
                {
                    parentEnode.Properties.Remove(name);
                }
            }
        }
예제 #5
0
 public bool IsResourceDictionary(ElementNode node) => false;
예제 #6
0
 static bool IsVisualStateGroupList(ElementNode node) => node?.XmlType.Name == "VisualStateGroup" && node?.Parent is IListNode;
예제 #7
0
 public void Visit(ElementNode node, INode parentNode) => action(node, parentNode);