public static IElementInitializer Create(IPropertyAdapter propertyAdapter, IEnumerable <object> values, XamlNamespaces namespaces)
        {
            if (!values.Any())
            {
                return(ElementInitializer.Empty);
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(propertyAdapter.PropertyType) &&
                !(values.Count() == 1 && values.First() is XamlElement && propertyAdapter.PropertyType.IsAssignableFrom(((XamlElement)values.First()).GetElementType())))
            {
                IElementInitializer propertyContentInitializer = ElementCollectionContentInitailizer.Create(values, propertyAdapter.PropertyType);

                // wrap with a factory that creates the collection (when it's null) before adding its values
                return(new ElementPropertyMemberFactoryInitializer(propertyAdapter, propertyContentInitializer));
            }

            if (values.Count() == 1)
            {
                if (propertyAdapter.PropertyType == typeof(IFrameworkElementFactory))
                {
                    return(new FrameworkElementFactoryInitializer(propertyAdapter, ElementFactory.FromValue(values.First(), null, namespaces)));
                }

                IElementFactory contentFactory = ElementFactory.FromValue(values.First(), propertyAdapter.PropertyType, namespaces);
                return(new ElementPropertyMemberInitializer(propertyAdapter, contentFactory));
            }

            throw new Granular.Exception("Member of type \"{0}\" cannot have more than one child", propertyAdapter.PropertyType.Name);
        }
        private static IElementInitializer CreateContentInitializer(XamlElement element)
        {
            Type elementType = element.GetElementType();

            string contentPropertyName = PropertyAttribute.GetPropertyName <ContentPropertyAttribute>(elementType);

            if (!contentPropertyName.IsNullOrEmpty())
            {
                return(ElementMemberInitializer.Create(new XamlName(contentPropertyName), elementType, element.Values, element.Namespaces));
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(elementType))
            {
                return(ElementCollectionContentInitailizer.Create(element.Values, elementType));
            }

            return(null);
        }
Exemple #3
0
        public static IElementInitializer Create(IPropertyAdapter propertyAdapter, IEnumerable <object> values, XamlNamespaces namespaces, Uri sourceUri)
        {
            if (!values.Any())
            {
                return(ElementInitializer.Empty);
            }

            if (values.Count() == 1)
            {
                object value = values.First();

                if (propertyAdapter.PropertyType == typeof(IFrameworkElementFactory))
                {
                    return(new FrameworkElementFactoryInitializer(propertyAdapter, ElementFactory.FromValue(value, null, namespaces, sourceUri)));
                }

                Type valueType = value is XamlElement ? ((XamlElement)value).GetElementType() : value.GetType();

                ITypeConverter typeConverter;
                if (propertyAdapter.PropertyType.IsAssignableFrom(valueType) || typeof(IMarkupExtension).IsAssignableFrom(valueType) || TypeConverter.TryGetTypeConverter(valueType, propertyAdapter.PropertyType, out typeConverter))
                {
                    IElementFactory contentFactory = ElementFactory.FromValue(value, propertyAdapter.PropertyType, namespaces, sourceUri);
                    return(new ElementPropertyMemberInitializer(propertyAdapter, contentFactory));
                }
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(propertyAdapter.PropertyType))
            {
                IElementInitializer propertyContentInitializer = ElementCollectionContentInitailizer.Create(values, propertyAdapter.PropertyType);

                // wrap with a factory that creates the collection (when it's null) before adding its values
                return(new ElementPropertyMemberFactoryInitializer(propertyAdapter, propertyContentInitializer));
            }

            if (values.Count() == 1)
            {
                object value = values.First();
                throw new Granular.Exception("Cannot assign value of type \"{0}\" to member of type \"{1}\"", value is XamlElement ? ((XamlElement)value).GetElementType() : value.GetType(), propertyAdapter.PropertyType.Name);
            }

            throw new Granular.Exception("Member of type \"{0}\" cannot have more than one child", propertyAdapter.PropertyType.Name);
        }
Exemple #4
0
        private static IElementInitializer CreateContentInitializer(XamlElement element, Type elementType)
        {
            if (!element.Values.Any())
            {
                return(null);
            }

            string contentPropertyName = ContentPropertyAttribute.GetPropertyName(elementType);

            if (!contentPropertyName.IsNullOrEmpty())
            {
                return(ElementMemberInitializer.Create(elementType, contentPropertyName, element.Values, element.Namespaces, element.SourceUri));
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(elementType))
            {
                return(ElementCollectionContentInitailizer.Create(element.Values, elementType));
            }

            throw new Granular.Exception("Cannot add content to element of type \"{0}\" as it's not a collection type and does not declare ContentProperty", elementType.Name);
        }
Exemple #5
0
        public static IElementFactory FromXamlElement(XamlElement element, Type targetType)
        {
            Type elementType = element.GetElementType();

            if (element.Values.Any() && ContentPropertyAttribute.GetPropertyName(elementType).IsNullOrEmpty() && !ElementCollectionContentInitailizer.IsCollectionType(elementType))
            {
                return(FromElementFactory(FromXamlElementContent(element), targetType, element.Namespaces, element.SourceUri));
            }

            IElementInitializer elementInitializer = new ElementInitializer(element);
            IElementFactory     elementFactory     = new ElementFactory(elementType, elementInitializer);

            return(FromElementFactory(elementFactory, targetType, element.Namespaces, element.SourceUri));
        }