public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
 {
     if (node is XamlAstClrProperty prop)
     {
         if (prop.DeclaringType.Assembly == context.GetWinUITypes().WinUIControlsAssembly)
         {
             IXamlField propertyIndexMaybe = context.GetWinUITypes().XamlPropertyIndex.Fields.FirstOrDefault(f => f.Name == $"{prop.DeclaringType.Name}_{prop.Name}");
             if (propertyIndexMaybe != null)
             {
                 prop.Setters.Insert(0, new XamlDirectSetter(context.GetWinUITypes(), prop.Getter.ReturnType, prop.DeclaringType, propertyIndexMaybe));
                 foreach (var adder in XamlTransformHelpers.FindPossibleAdders(context, prop.Getter.ReturnType))
                 {
                     if (adder.Parameters.Count == 1)
                     {
                         prop.Setters.Add(new XamlDirectAdderSetter(context.GetWinUITypes(), adder.Parameters[0], prop.DeclaringType, propertyIndexMaybe));
                     }
                 }
                 return(prop);
             }
             IXamlField eventIndexMaybe = context.GetWinUITypes().XamlEventIndex.Fields.FirstOrDefault(f => f.Name == $"{prop.DeclaringType.Name}_{prop.Name}");
             if (eventIndexMaybe != null)
             {
                 prop.Setters.Insert(0, new XamlDirectEventSetter(context.GetWinUITypes(), prop.Setters[0].Parameters[0], prop.DeclaringType, eventIndexMaybe));
                 return(prop);
             }
         }
     }
     return(node);
 }
Beispiel #2
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstObjectNode ni)
            {
                XamlAstXamlPropertyValueNode propertyNode = null;

                for (var c = ni.Children.Count - 1; c >= 0; c--)
                {
                    var child = ni.Children[c];
                    if (child is IXamlAstValueNode valueNode)
                    {
                        if (propertyNode == null)
                        {
                            var contentProperty = context.Configuration.FindContentProperty(ni.Type.GetClrType());
                            if (contentProperty != null)
                            {
                                propertyNode = new XamlAstXamlPropertyValueNode(ni,
                                                                                new XamlAstClrProperty(ni, contentProperty, context.Configuration),
                                                                                Array.Empty <IXamlAstValueNode>());
                            }
                            else
                            {
                                var adders = XamlTransformHelpers.FindPossibleAdders(context, ni.Type.GetClrType());
                                if (adders.Count == 0)
                                {
                                    // If there's no content property, strip all whitespace-only nodes and continue
                                    WhitespaceNormalization.RemoveWhitespaceNodes(ni.Children);
                                    if (!ni.Children.Contains(child))
                                    {
                                        continue;
                                    }

                                    throw new XamlParseException(
                                              $"No Content property or any Add methods found for type {ni.Type.GetClrType().GetFqn()}",
                                              child);
                                }

                                propertyNode = new XamlAstXamlPropertyValueNode(ni, new XamlAstClrProperty(ni,
                                                                                                           "Content", ni.Type.GetClrType(), null,
                                                                                                           adders.Select(a => new XamlDirectCallPropertySetter(a)
                                {
                                    BinderParameters = { AllowMultiple = true }
                                })),
                                                                                Array.Empty <IXamlAstValueNode>());
                            }
                        }
                        // We are going in reverse order, so insert at the beginning
                        propertyNode.Values.Insert(0, valueNode);
                        ni.Children.RemoveAt(c);
                    }
                }

                if (propertyNode != null)
                {
                    ni.Children.Add(propertyNode);
                }
            }

            return(node);
        }
Beispiel #3
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstClrProperty prop && prop.Getter != null)
            {
                foreach (var adder in XamlTransformHelpers.FindPossibleAdders(context, prop.Getter.ReturnType))
                {
                    prop.Setters.Add(new AdderSetter(prop.Getter, adder));
                }
            }

            return(node);
        }