示例#1
0
            private static IElementFactory GetDeferredKeyFactory(XamlElement xamlElement)
            {
                Type elementType = xamlElement.GetElementType();

                IDeferredValueKeyProvider provider = DeferredValueKeyProviders.GetDeferredValueKeyProvider(elementType);

                if (provider != null)
                {
                    return(new DeferredKeyFactory(provider, xamlElement));
                }

                string keyPropertyName = DictionaryKeyPropertyAttribute.GetPropertyName(elementType);

                if (!keyPropertyName.IsNullOrWhiteSpace())
                {
                    XamlMember keyMember = xamlElement.Members.FirstOrDefault(member => member.Name.LocalName == keyPropertyName);
                    if (keyMember != null)
                    {
                        IPropertyAdapter keyProperty = PropertyAdapter.CreateAdapter(elementType, keyPropertyName);
                        return(ElementFactory.FromValue(keyMember.Values.Single(), keyProperty.PropertyType, xamlElement.Namespaces, xamlElement.SourceUri));
                    }
                }

                return(null);
            }
        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);
        }
示例#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);
        }
            private static IElementFactory GetKeyDirectiveFactory(XamlElement element, Type keyType)
            {
                XamlMember keyDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.KeyDirective);

                return(keyDirective != null?ElementFactory.FromValue(keyDirective.GetSingleValue(), keyType, element.Namespaces) : null);
            }
 public ElementCollectionContentInitializer(Type valueTargetType, IEnumerable <object> values)
 {
     elementsFactory = values.Select(value => ElementFactory.FromValue(value, valueTargetType, XamlNamespaces.Empty)).ToArray();
 }