Exemplo n.º 1
0
        private static IEnumerable <KeyValueElementFactory> CreateElementsFactories(Type keyType, Type valueType, IEnumerable <object> values)
        {
            if (values.Any(value => !(value is XamlElement)))
            {
                throw new Granular.Exception("Can't add a value of type \"{0}\" to a dictionary, as it cannot have a key", values.First(value => !(value is XamlElement)).GetType().Name);
            }

            IEnumerable <XamlElement> valuesElements = values.Cast <XamlElement>();

            List <KeyValueElementFactory> list = new List <KeyValueElementFactory>();

            foreach (XamlElement contentChild in valuesElements)
            {
                bool isShared = contentChild.Directives.All(directive => directive.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(directive.GetSingleValue(), typeof(bool), XamlNamespaces.Empty));

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

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

                list.Add(new KeyValueElementFactory(keyType, contentChildFactory, contentChild));
            }

            return(list);
        }
Exemplo n.º 2
0
            public object CreateElement(InitializeContext context)
            {
                object value = null;

                return(new ValueProvider(() =>
                {
                    if (elementFactory == null)
                    {
                        elementFactory = ElementFactory.FromXamlElement(element, targetType);
                    }

                    if (value == null || !isShared)
                    {
                        value = elementFactory.CreateElement(context);
                    }

                    return value;
                }));
            }
Exemplo n.º 3
0
        public static object Load(XamlElement resource)
        {
            IElementFactory factory = ElementFactory.FromXamlElement(resource, null);

            return(factory.CreateElement(new InitializeContext()));
        }
Exemplo n.º 4
0
        private static IEnumerable <KeyValueElementFactory> CreateElementsFactories(Type dictionaryType, Type keyType, Type valueType, IEnumerable <object> values)
        {
            if (values.Any(value => !(value is XamlElement)))
            {
                throw new Granular.Exception("Can't add a value of type \"{0}\" to a dictionary, as it cannot have a key", values.First(value => !(value is XamlElement)).GetType().Name);
            }

            IEnumerable <XamlElement> valuesElements = System.Linq.Enumerable.Cast <XamlElement>(values);

            bool isValueProviderSupported = dictionaryType.GetCustomAttributes(true).OfType <SupportsValueProviderAttribute>().Any();

            List <KeyValueElementFactory> list = new List <KeyValueElementFactory>();

            foreach (XamlElement contentChild in valuesElements)
            {
                bool isShared = contentChild.Directives.All(directive => directive.Name != XamlLanguage.SharedDirective || (bool)TypeConverter.ConvertValue(directive.GetSingleValue(), typeof(bool), XamlNamespaces.Empty, null));

                if (!isShared && !isValueProviderSupported)
                {
                    throw new Granular.Exception($"Can't add a non shared value to \"{dictionaryType.FullName}\" as it does not declare a \"SupportsValueProvider\" attribute");
                }

                IElementFactory contentChildFactory = isValueProviderSupported ? new DeferredValueFactory(contentChild, valueType, isShared) : ElementFactory.FromXamlElement(contentChild, valueType);

                list.Add(new KeyValueElementFactory(keyType, contentChildFactory, contentChild, isValueProviderSupported));
            }

            return(list);
        }