Пример #1
0
        public IStyleRule?GetRule(IMarkupNode markupNode,
                                  Type targetType)
        {
            if (markupNode.TryGetAttributeValue(nameof(DependencyPropertySelector.Property),
                                                out var propertyName))
            {
                if (DependencyProperty.TryGetDependecyProperty(targetType, propertyName, out var depProp))
                {
                    var selector = new DependencyPropertySelector(depProp);

                    var propVal = BuildPropertyValue(depProp, markupNode);

                    var objDeclaration = new ObjectDeclaration(propVal);

                    return(new DependencyPropertyValueRule(selector, objDeclaration));
                }
            }

            throw new NotImplementedException();
        }
Пример #2
0
        public Type GetType(IMarkupNode node,
                            String?genericArgName,
                            Dictionary <String, String> nameSpaceAssemblySearch)
        {
            var name = node.Name;

            if (_typeAliases.TryGetValue(name, out var aliasedType))
            {
                return(node.ChildrenCount == 0
                    ? aliasedType.ChildlessType
                    : aliasedType.PanelType);
            }

            if (name == "input" && node.TryGetAttributeValue("type", out var inputType))
            {
                switch (inputType)
                {
                case "checkbox":
                    return(typeof(CheckBox));
                }
            }

            var notGeneric = _typeInferrer.GetTypeFromClearName(name, nameSpaceAssemblySearch);

            if (notGeneric != null)
            {
                return(notGeneric);
            }

            if (!String.IsNullOrEmpty(genericArgName) && node.ChildrenCount > 0)
            {
                var letsTry = name + "[" + genericArgName + "]";
                var found   = _typeInferrer.GetTypeFromClearName(letsTry, nameSpaceAssemblySearch, true);
                if (found != null)
                {
                    return(found);
                }
            }

            throw new TypeLoadException(name);
        }
Пример #3
0
        private async Task <IContentVisual> BuildContentVisualAsync(IMarkupNode node,
                                                                    Type?dataContextType,
                                                                    Dictionary <String, String> nameSpaceAssemblySearch,
                                                                    Type visualType,
                                                                    IVisualLineage visualLineage,
                                                                    ApplyVisualStyles applyStyles)
        {
            IVisualElement?contentVisual = null;

            //-------------------------------
            var contentContainer = _visualBootstrapper.Instantiate <IContentVisual>(visualType)
                                   ?? throw new InvalidOperationException();

            //await applyStyles(contentContainer, node, visualLineage, this);
            //-------------------------------

            visualLineage.PushVisual(contentContainer);

            switch (node.ChildrenCount)
            {
            case 1:
            {
                var currentNode = node[0];
                var childObjRes = await InflateChildAsync(currentNode, contentContainer,
                                                          dataContextType, nameSpaceAssemblySearch, visualLineage, applyStyles)
                                  .ConfigureAwait(false);

                if (childObjRes.ChildType == ChildNodeType.ChildVisual &&
                    childObjRes.Child is IVisualElement childVisual)
                {
                    contentVisual = childVisual;
                }

                else if (childObjRes.ChildType == ChildNodeType.PropertyValue &&
                         childObjRes.VisualProperty is { } prop)
                {
                    //Object oContentContainer = contentContainer;
                    prop.SetPropertyValue(ref contentContainer, childObjRes.Child);
                    //prop.SetValue(contentContainer, childObjRes.Child, null);
                }

                break;
            }

            case 0:
            {
                if (!node.TryGetAttributeValue(nameof(IContentContainer.Content), out var textContent))
                {
                    textContent = node.InnerText;
                }

                if (!String.IsNullOrEmpty(textContent))
                {
                    // zb <button>TEXT</button> etc
                    contentVisual = new Label(_visualBootstrapper)
                    {
                        Text = textContent !
                    };
                    visualLineage.PushVisual(contentVisual);
                }

                break;
            }

            default:
                throw new NotImplementedException();
            }


            contentContainer !.Content = contentVisual;

            if (contentVisual != null)
            {
                visualLineage.AssertPopVisual(contentVisual);
            }


            return(contentContainer);
        }
Пример #4
0
        /// <summary>
        ///     Builds a visual from a markup node.  Infers the data context type and instantiates
        ///     a generic visual if possible
        /// </summary>
        /// <param name="node">an xml/json etc node</param>
        /// <param name="dataContextType">the data context type of the parent visual.</param>
        /// <param name="nameSpaceAssemblySearch"></param>
        /// <param name="visualLineage"></param>
        /// <param name="applyStyles"></param>
        /// <returns></returns>
        private async Task <IVisualElement> GetVisualAsync(IMarkupNode node,
                                                           Type?dataContextType,
                                                           Dictionary <String, String> nameSpaceAssemblySearch,
                                                           IVisualLineage visualLineage,
                                                           ApplyVisualStyles applyStyles)
        {
            var bindings = await _bindingBuilder.GetBindingsDictionaryAsync(node,
                                                                            dataContextType, nameSpaceAssemblySearch);

            var dataBindings = bindings.Values.OfType <IDataBinding>()
                               .ToArray();

            dataContextType = _bindingBuilder.InferDataContextTypeFromBindings(dataBindings,
                                                                               dataContextType);

            if (!node.TryGetAttributeValue("ContextType", out var currentGenericArgName))
            {
                currentGenericArgName = dataContextType?.Name;
            }
            else
            {
                dataContextType = _typeInferrer.GetTypeFromClearName(currentGenericArgName,
                                                                     nameSpaceAssemblySearch, true);
            }

            var visualType = _visualTypeResolver.GetType(node, currentGenericArgName,
                                                         nameSpaceAssemblySearch);

            IVisualElement visual;

            if (typeof(IContentContainer).IsAssignableFrom(visualType))
            {
                // CONTENT VISUAL
                visual = await BuildContentVisualAsync(node, dataContextType,
                                                       nameSpaceAssemblySearch, visualType, visualLineage, applyStyles)
                         .ConfigureAwait(false);
            }
            else if (visualType is { } validVisualType)
            {
                //-------------------------------
                visual = _visualBootstrapper.Instantiate <IVisualElement>(validVisualType);
                //await applyStyles(visual, node, visualLineage, this);
                //-------------------------------

                visualLineage.PushVisual(visual);

                if (node.ChildrenCount > 0)
                {
                    // PANEL
                    await InflateAndAddChildNodesAsync(node, visual, dataContextType,
                                                       nameSpaceAssemblySearch, visualLineage,
                                                       _appliedStyleBuilder.ApplyVisualStylesAsync).ConfigureAwait(false);
                }

                if (node.InnerText is { } innerText&&
                    innerText.Trim() is { } validInnerText&& validInnerText.Length > 0 &&
                    GetAttribute <ContentPropertyAttribute>(validVisualType) is { } cp&&
                    _typeInferrer.FindPublicProperty(validVisualType, cp.Name) is { } contentProp&&
                    contentProp.PropertyType == typeof(String))
                {
                    // zb <Label>hello world</Label>
                    contentProp.SetValue(visual, validInnerText, null);
                }
            }