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); }
public ElementInitializer(XamlElement element) { elementType = element.GetElementType(); namespaces = element.Namespaces; memberInitializers = CreateMemberInitializers(element); contentInitializer = CreateContentInitializer(element); nameDirectiveValue = GetNameDirectiveValue(element); nameProperty = GetNameProperty(element.GetElementType()); }
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); }
public ElementPropertyMemberFactoryInitializer(IPropertyAdapter propertyAdapter, IElementInitializer propertyContentInitializer) { this.propertyAdapter = propertyAdapter; this.propertyValueInitializer = propertyContentInitializer; }
public ElementFactory(Type elementType, IElementInitializer elementInitializer) { this.ElementType = elementType; this.elementInitializer = elementInitializer; }