Beispiel #1
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);
        }
Beispiel #2
0
        private static IEnumerable <IVisualElement> GetSelectableVisualsImpl(IVisualElement rootVisual,
                                                                             AndStyleSelector selectors,
                                                                             IVisualLineage visualLineage,
                                                                             Int32 selectorIndex)
        {
            var currentVisual = rootVisual;

            for (var c = selectorIndex; c < selectors.Count; c++)
            {
                var currentSelector = selectors[c];

                switch (currentSelector)
                {
                case CombinatorSelector combinator:
                    switch (combinator.Combinator)
                    {
                    case Combinator.Invalid:
                    case Combinator.None:
                        throw new InvalidOperationException();

                    case Combinator.Descendant:
                        break;

                    case Combinator.Child:
                        if (!(currentVisual is IVisualContainer container))
                        {
                            yield break;
                        }

                        foreach (var childVisual in container.Children.GetAllChildren())
                        {
                            visualLineage.PushVisual(childVisual);

                            var selectableChildren = GetSelectableVisualsImpl(childVisual,
                                                                              selectors, visualLineage, c + 1);

                            foreach (var selectable in selectableChildren)
                            {
                                yield return(selectable);
                            }

                            visualLineage.AssertPopVisual(childVisual);
                        }

                        break;


                    case Combinator.GeneralSibling:
                        break;

                    case Combinator.AdjacentSibling:

                        var nextSibling = visualLineage.GetNextSibling();

                        if (nextSibling != null)
                        {
                            visualLineage.PushVisual(nextSibling);

                            var selectableSiblings = GetSelectableVisualsImpl(nextSibling,
                                                                              selectors, visualLineage, c + 1);

                            foreach (var selectable in selectableSiblings)
                            {
                                yield return(selectable);
                            }

                            visualLineage.AssertPopVisual(nextSibling);
                        }

                        break;

                    case Combinator.Column:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                    break;

                case VisualStateSelector stateSelector:
                    if (IsVisualSelectable(currentVisual, stateSelector.BaseSelector, visualLineage))
                    {
                    }
                    goto default;

                default:
                    if (!IsVisualSelectable(currentVisual, currentSelector, visualLineage))
                    {
                        yield break;
                    }

                    if (c == selectors.Count - 1)
                    {
                        yield return(currentVisual);
                    }
                    break;
                }
            }
        }
Beispiel #3
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);
                }
            }