Пример #1
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (!(node is XamlAstObjectNode on))
            {
                return(node);
            }
            var nonDirectiveChildren = on.Children.Where(a => !(a is XamlAstXmlDirective)).ToList();

            if (on.Arguments.Count != 0 ||
                nonDirectiveChildren.Count != 1 ||
                !(nonDirectiveChildren[0] is IXamlAstValueNode vn) ||
                !vn.Type.GetClrType().Equals(context.Configuration.WellKnownTypes.String))
            {
                return(node);
            }

            if (XamlTransformHelpers.TryGetCorrectlyTypedValue(context, vn, on.Type.GetClrType(), out var rv))
            {
                if (nonDirectiveChildren.Count != on.Children.Count)
                {
                    rv = new XamlValueWithManipulationNode(rv, rv,
                                                           new XamlManipulationGroupNode(rv, on.Children.OfType <XamlAstXmlDirective>()));
                }
                return(rv);
            }

            if (on.Type.GetClrType().IsValueType)
            {
                throw new XamlLoadException(
                          $"Unable to convert value {(vn as XamlAstTextNode)?.Text}) to {on.Type.GetClrType()}", vn);
            }

            // Parser not found, isn't a value type, probably a regular object creation node with text content
            return(node);
        }
Пример #2
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlValueWithManipulationNode valWithManip &&
                TryGetNewObjectNodeForXamlDirect(valWithManip, out var newObj) &&
                newObj.Arguments.Count == 0)
            {
                if (!(valWithManip.Manipulation is XamlObjectInitializationNode init) ||
                    !(init.Manipulation is XamlManipulationGroupNode manipGrp))
                {
                    throw new XamlParseException(
                              "Unable to find the object initialization node inside object creation.", node);
                }

                IXamlType objType = newObj.Type.GetClrType();
                if (objType.Assembly != context.GetWinUITypes().WinUIControlsAssembly)
                {
                    // Only built-in controls have XamlDirect support.
                    // Remove the XamlDirect setters since XamlDirect isn't worth using once the object
                    // is created and return.
                    RemoveXamlDirectSettersFromAssignments(manipGrp);
                    return(node);
                }
                IXamlField typeIndex = context.GetWinUITypes().XamlTypeIndex.GetAllFields().FirstOrDefault(fld => fld.Name == objType.Name);
                if (typeIndex is null)
                {
                    // We didn't find a matching type index, so we can't use XamlDirect.
                    // Remove the XamlDirect setters since XamlDirect isn't worth using once the object
                    // is created and return.
                    RemoveXamlDirectSettersFromAssignments(manipGrp);

                    return(node);
                }
                IXamlAstValueNode constructedObjNode = new XamlDirectNewObjectNode(newObj, context.GetWinUITypes().IXamlDirectObject, typeIndex);
                var xamlDirectAssignmentsNode        = new XamlManipulationGroupNode(constructedObjNode);
                var standardAssignmentsNode          = new XamlManipulationGroupNode(constructedObjNode);

                ExtractXamlDirectAssignments(manipGrp, xamlDirectAssignmentsNode, standardAssignmentsNode);

                if (xamlDirectAssignmentsNode.Children.Count != 0)
                {
                    constructedObjNode = new XamlValueWithManipulationNode(constructedObjNode, constructedObjNode, xamlDirectAssignmentsNode);
                }

                if (standardAssignmentsNode.Children.Count == 0)
                {
                    node = new XamlObjectFromDirectObjectNode(constructedObjNode, newObj.Type);
                }
                else
                {
                    valWithManip.Value = new XamlObjectFromDirectObjectNode(constructedObjNode, newObj.Type);
                    init.Manipulation  = standardAssignmentsNode;
                }
            }
            return(node);