示例#1
0
        private static IEnumerable<KeyElementFactory> CreateElementsFactories(XamlElement memberElement)
        {
            List<KeyElementFactory> list = new List<KeyElementFactory>();

            foreach (XamlElement contentChild in memberElement.GetContentChildren())
            {
                bool isShared = contentChild.Attributes.All(attribute => attribute.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(attribute.Value, typeof(bool), XamlNamespaces.Empty));

                IElementFactory contentChildFactory = ElementFactory.FromXamlElement(contentChild, null);

                if (!isShared)
                {
                    contentChildFactory = new ValueProviderFactory(contentChildFactory);
                }

                list.Add(new KeyElementFactory(contentChildFactory, contentChild));
            }

            return list;
        }
示例#2
0
 public ElementListContentInitializer(XamlElement element)
 {
     elementsFactory = element.GetContentChildren().Select(contentChild => ElementFactory.FromXamlElement(contentChild, null)).ToArray();
 }
示例#3
0
        public static IElementInitializer FromXamlElement(IPropertyAdapter propertyAdapter, XamlElement memberElement)
        {
            IEnumerable<XamlElement> children = memberElement.GetContentChildren();

            if (!children.Any())
            {
                if (!memberElement.TextValue.IsNullOrEmpty())
                {
                    object value = TypeConverter.ConvertValue(memberElement.TextValue, propertyAdapter.PropertyType, memberElement.Namespaces);
                    return new ElementPropertyMemberInitializer(propertyAdapter, new ConstantElementFactory(value));
                }

                return ElementInitializer.Empty;
            }

            if (ElementCollectionContentInitailizer.IsCollectionType(propertyAdapter.PropertyType) &&
                !(children.Count() == 1 && propertyAdapter.PropertyType.IsAssignableFrom(children.First().GetElementType())))
            {
                IElementInitializer propertyContentInitializer = ElementCollectionContentInitailizer.FromXamlElement(memberElement, propertyAdapter.PropertyType);
                // use a factory that creates the property value before initializing its content (when its null)
                return new ElementPropertyMemberFactoryInitializer(propertyAdapter, propertyContentInitializer);
            }

            if (children.Count() == 1)
            {
                if (propertyAdapter.PropertyType == typeof(IFrameworkElementFactory))
                {
                    return new FrameworkElementFactoryInitializer(propertyAdapter, ElementFactory.FromXamlElement(children.First(), children.First().GetElementType()));
                }

                IElementFactory contentFactory = ElementFactory.FromXamlElement(children.First(), propertyAdapter.PropertyType);
                return new ElementPropertyMemberInitializer(propertyAdapter, contentFactory);
            }

            throw new Granular.Exception("Element \"{0}\" cannot have more than one child", memberElement.Name);
        }
示例#4
0
        private static IElementFactory FromXamlElementContent(XamlElement element)
        {
            if (element.GetMemberNodes().Any())
            {
                throw new Granular.Exception("Element \"{0}\" can't have members, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
            }

            IEnumerable<XamlElement> contentChilren = element.GetContentChildren();

            if (contentChilren.Any() && !element.TextValue.IsNullOrEmpty())
            {
                throw new Granular.Exception("Element \"{0}\" cannot have both children and text value", element.Name);
            }

            if (!contentChilren.Any())
            {
                return new ConvertedElementFactory(new ConstantElementFactory(element.TextValue), element.GetElementType(), element.Namespaces);
            }

            if (contentChilren.Count() == 1)
            {
                return ElementFactory.FromXamlElement(contentChilren.First(), element.GetElementType());
            }

            throw new Granular.Exception("Element \"{0}\" can't have multiple children, as its type doesn't have a default constructor and it can only be converted from its content", element.Name);
        }