예제 #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);
            }
예제 #2
0
        public ElementInitializer(XamlElement element)
        {
            elementType = element.GetElementType();
            namespaces  = element.Namespaces;

            memberInitializers = CreateMemberInitializers(element);
            contentInitializer = CreateContentInitializer(element);

            nameDirectiveValue = GetNameDirectiveValue(element);
            nameProperty       = GetNameProperty(element.GetElementType());
        }
예제 #3
0
        private XamlElement Parse()
        {
            this.tokens = new ReadOnlyStack <Token>(lexer.GetTokens(text));

            XamlElement root = MatchElement();

            if (!tokens.IsEmpty)
            {
                throw new Granular.Exception("Can't parse \"{0}\", end of stream is expected at index {1}", text, tokens.Peek().Start);
            }

            return(root);
        }
예제 #4
0
        public static IElementFactory FromXamlElement(XamlElement element, Type targetType)
        {
            Type elementType = element.GetElementType();

            if (elementType.GetDefaultConstructor() == null)
            {
                return(FromElementFactory(FromXamlElementContent(element), targetType, element.Namespaces));
            }

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

            return(FromElementFactory(elementFactory, targetType, element.Namespaces));
        }
예제 #5
0
        private static IElementFactory FromXamlElementContent(XamlElement element)
        {
            if (element.Members.Any())
            {
                throw new Granular.Exception("Element \"{0}\" can't have members, as it's not a collection type and does not declare ContentProperty and can only be converted from its content", element.Name);
            }

            if (element.Values.Count() > 1)
            {
                throw new Granular.Exception("Element \"{0}\" can't have multiple children, as it's not a collection type and does not declare ContentProperty and can only be converted from its content", element.Name);
            }

            return(FromValue(element.Values.First(), element.GetElementType(), element.Namespaces, element.SourceUri));
        }
예제 #6
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));
        }
예제 #7
0
        private static IEnumerable <IElementInitializer> CreateMemberInitializers(XamlElement element, Type elementType)
        {
            List <IElementInitializer> list = new List <IElementInitializer>();

            int index = 0;

            foreach (XamlMember member in element.Members)
            {
                // markup extensions may contain members with an empty name, the name should be resolved from the member index
                XamlName memberName = member.Name.IsEmpty ? GetParameterName(elementType, index) : member.Name;

                list.Add(ElementMemberInitializer.Create(memberName.ResolveContainingType(elementType), memberName.MemberName, member.Values, member.Namespaces, member.SourceUri));
                index++;
            }

            return(list);
        }
예제 #8
0
        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);
        }
예제 #9
0
        private static IElementFactory FromXamlElementContent(XamlElement element)
        {
            if (element.Members.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);
            }

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

            if (element.Values.Count() > 1)
            {
                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);
            }

            return(FromValue(element.Values.First(), element.GetElementType(), element.Namespaces));
        }
예제 #10
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);
        }
예제 #11
0
파일: XamlLoader.cs 프로젝트: Ref12/Grazor
        public static void Load(object target, XamlElement resource)
        {
            IElementInitializer initializer = new ElementInitializer(resource);

            initializer.InitializeElement(target, new InitializeContext());
        }
예제 #12
0
파일: XamlLoader.cs 프로젝트: Ref12/Grazor
        public static object Load(XamlElement resource)
        {
            IElementFactory factory = ElementFactory.FromXamlElement(resource, null);

            return(factory.CreateElement(new InitializeContext()));
        }
예제 #13
0
 public static Type GetElementType(this XamlElement element)
 {
     return(TypeParser.ParseType(element.Name));
 }
예제 #14
0
            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);
            }
예제 #15
0
            public KeyValueElementFactory(Type keyType, IElementFactory valueFactory, XamlElement xamlElement)
            {
                this.valueFactory = valueFactory;

                keyProperty         = GetKeyProperty(valueFactory.ElementType);
                keyDirectiveFactory = GetKeyDirectiveFactory(xamlElement, keyType);

                if (keyDirectiveFactory == null && keyProperty == null)
                {
                    throw new Granular.Exception("Dictionary item \"{0}\" must have a key", xamlElement.Name);
                }
            }
예제 #16
0
        private static string GetNameDirectiveValue(XamlElement element)
        {
            XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective);

            return(nameDirective != null ? (string)nameDirective.GetSingleValue() : null);
        }
예제 #17
0
 public DeferredKeyFactory(IDeferredValueKeyProvider provider, XamlElement element)
 {
     this.provider = provider;
     this.element  = element;
 }
예제 #18
0
 public DeferredValueFactory(XamlElement element, Type targetType, bool isShared)
 {
     this.element    = element;
     this.targetType = targetType;
     this.isShared   = isShared;
 }