public void Visit(ElementNode node, INode parentNode)
        {
            if (!Values.TryGetValue(node, out var value) && Context.ExceptionHandler != null)
            {
                return;
            }

            //Set RD to VE
            if (typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]) && ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName propertyName))
            {
                if ((propertyName.LocalName == "Resources" ||
                     propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) && value is ResourceDictionary)
                {
                    var source = Values[parentNode];
                    ApplyPropertiesVisitor.SetPropertyValue(source, propertyName, value, Context.RootElement, node, Context, node);
                    return;
                }
            }

            //Only proceed further if the node is a keyless RD
            if (parentNode is IElementNode &&
                Context.Types.TryGetValue((IElementNode)parentNode, out var parentType) &&
                typeof(ResourceDictionary).IsAssignableFrom(parentType) &&
                !((IElementNode)parentNode).Properties.ContainsKey(XmlName.xKey))
            {
                node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
            }
            else if (parentNode is ListNode &&
                     typeof(ResourceDictionary).IsAssignableFrom(Context.Types[((IElementNode)parentNode.Parent)]) &&
                     !((IElementNode)parentNode.Parent).Properties.ContainsKey(XmlName.xKey))
            {
                node.Accept(new ApplyPropertiesVisitor(Context, stopOnResourceDictionary: false), parentNode);
            }
        }
Пример #2
0
 public void Visit(ListNode node, INode parentNode)
 {
     //this is a gross hack to keep ListNode alive. ListNode must go in favor of Properties
     if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name))
     {
         node.XmlName = name;
     }
 }
Пример #3
0
        private void SetPropertyValue(string prop, string strValue, object value, IServiceProvider serviceProvider)
        {
            MethodInfo setter;

            if (prop == null)
            {
                //implicit property
                var t = markupExtension.GetType();
                prop = ApplyPropertiesVisitor.GetContentPropertyName(t.GetTypeInfo());
                if (prop == null)
                {
                    return;
                }
                try {
                    setter = t.GetRuntimeProperty(prop).SetMethod;
                }
                catch (AmbiguousMatchException e) {
                    throw new XamlParseException($"Multiple properties with name  '{t}.{prop}' found.", serviceProvider, innerException: e);
                }
            }
            else
            {
                try {
                    setter = markupExtension.GetType().GetRuntimeProperty(prop).SetMethod;
                }
                catch (AmbiguousMatchException e) {
                    throw new XamlParseException($"Multiple properties with name  '{markupExtension.GetType()}.{prop}' found.", serviceProvider, innerException: e);
                }
            }
            if (value == null && strValue != null)
            {
                try {
                    value = strValue.ConvertTo(markupExtension.GetType().GetRuntimeProperty(prop).PropertyType,
                                               (Func <TypeConverter>)null, serviceProvider, out Exception converterException);
                    if (converterException != null)
                    {
                        throw converterException;
                    }
                }
                catch (AmbiguousMatchException e) {
                    throw new XamlParseException($"Multiple properties with name  '{markupExtension.GetType()}.{prop}' found.", serviceProvider, innerException: e);
                }
            }

            setter.Invoke(markupExtension, new[] { value });
        }
Пример #4
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)
                    {
                        try {
                            value = Activator.CreateInstance(type);
                        }
                        catch (Exception e) when(e is TargetInvocationException || e is MemberAccessException)
                        {
                            value = XamlLoader.InstantiationFailedCallback?.Invoke(new XamlLoader.CallbackTypeInfo {
                                XmlNamespace = node.XmlType.NamespaceUri, XmlTypeName = node.XmlType.Name
                            }, type, e) ?? throw e;
                        }
                    }
                }
                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);
            }

            if (XamlLoader.ValueCreatedCallback != null)
            {
                var name = node.XmlType.Name;
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                XamlLoader.ValueCreatedCallback(new XamlLoader.CallbackTypeInfo {
                    XmlNamespace = node.XmlType.NamespaceUri, XmlTypeName = name
                }, value);
            }
        }