Ejemplo n.º 1
0
        BindingBase IMarkupExtension <BindingBase> .ProvideValue(IServiceProvider serviceProvider)
        {
            var targetElement  = serviceProvider?.GetService <IProvideValueTarget>()?.TargetObject as BindableObject ?? throw new XamlParseException("Missing serviceProvider, or ProvideValueTarget, or TargetObject", serviceProvider?.GetService <IXmlLineInfoProvider>()?.XmlLineInfo ?? new XmlLineInfo());
            var targetProperty = serviceProvider?.GetService <IProvideValueTarget>()?.TargetProperty as BindableProperty ?? throw new XamlParseException("Missing serviceProvider, or ProvideValueTarget, or TargetObject", serviceProvider?.GetService <IXmlLineInfoProvider>()?.XmlLineInfo ?? new XmlLineInfo());

            var argument = Argument;

            if (GetVariable(argument, ArgumentBinding, targetElement))
            {
                argument = $"{Argument}: ${variableChangedHandler.VariableId}";
            }
            (var gqlField, var bindingPath) = ParseField(Field, argument);

            //When Binding to a bindingcontext, return a GraphQLObject. Same for ListView.ItemSource
            if (targetProperty == BindableObject.BindingContextProperty)
            {
                var gObject = new GraphQLObject {
                    EndPoint = EndPoint, Field = gqlField
                };
                if (Extra != null)
                {
                    ((IGraphQLObject)gObject).AddQueryField(new GraphQLField(Extra));
                }
                if (variableChangedHandler != null)
                {
                    ((IGraphQLObject)gObject).AddVariable(variableChangedHandler);
                }
                return(new Binding(".", mode: BindingMode.OneWay, converter: new GraphQLObjectConverter(), converterParameter: gObject));
            }
            if (TemplatableProperties.TryGetValue(targetProperty, out var itemTemplateProperty))
            {
                var gObject = new GraphQLObject {
                    EndPoint = EndPoint, Field = gqlField
                };
                if (Extra != null)
                {
                    ((IGraphQLObject)gObject).AddQueryField(new GraphQLField(Extra));
                }
                if (variableChangedHandler != null)
                {
                    ((IGraphQLObject)gObject).AddVariable(variableChangedHandler);
                }
                ApplyContextToMockTemplatedElement(targetElement, itemTemplateProperty, gObject);
                return(new Binding(".", mode: BindingMode.OneWay, converter: new GraphQLObjectConverter(), converterParameter: gObject));
            }
            if (Extra != null)
            {
                gqlField.SubFields = new List <IGraphQLField> {
                    new GraphQLField(Extra)
                }
            }
            ;
            (targetElement.BindingContext as IGraphQLObject)?.AddQueryField(gqlField);
            if (variableChangedHandler != null)
            {
                (targetElement.BindingContext as IGraphQLObject)?.AddVariable(variableChangedHandler);
            }

            targetElement.BindingContextChanged += (o, e) => {
                (((BindableObject)o).BindingContext as IGraphQLObject)?.AddQueryField(gqlField);
                if (variableChangedHandler != null)
                {
                    (((BindableObject)o).BindingContext as IGraphQLObject)?.AddVariable(variableChangedHandler);
                }
            };

            return(new Binding($"{nameof(IGraphQLObject.Data)}{bindingPath}", BindingMode.OneWay, converter: new GraphQLValueConverter()));
        }

        object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) => (this as IMarkupExtension <BindingBase>).ProvideValue(serviceProvider);
Ejemplo n.º 2
0
        void ApplyContextToMockTemplatedElement(BindableObject targetElement, BindableProperty itemTemplateProperty, GraphQLObject gObject)
        {
            if (targetElement == null)
            {
                throw new ArgumentNullException(nameof(gObject));
            }
            if (itemTemplateProperty == null)
            {
                throw new ArgumentNullException(nameof(itemTemplateProperty));
            }
            if (gObject == null)
            {
                throw new ArgumentNullException(nameof(targetElement));
            }

            var mockElement = (targetElement.GetValue(itemTemplateProperty) as ElementTemplate)?.CreateContent() as BindableObject;

            if (mockElement != null)
            {
                mockElement.BindingContext = gObject;
                mockElement.BindingContext = null;
                mockElement = null;
            }

            targetElement.PropertyChanged += (o, e) => {
                if (e.PropertyName != itemTemplateProperty.PropertyName)
                {
                    return;
                }
                mockElement = (targetElement.GetValue(itemTemplateProperty) as ElementTemplate)?.CreateContent() as BindableObject;
                if (mockElement != null)
                {
                    mockElement.BindingContext = gObject;
                    mockElement.BindingContext = null;
                    mockElement = null;
                }
            };
        }