コード例 #1
0
            private bool IsChildableElement(XElement e, Type typeOfChildrenCollectionItems)
            {
                if (e.Attribute("Handler") != null)
                {
                    return(false); // this is an event handler, not a ui element
                }
                var localname = e.Name.LocalName;
                var finalname = GetLastSegment(localname);

                if (finalname.EndsWith('s'))
                {
                    // this is probably like "RowDefinitions" so the type is "RowDefinitionCollection"
                    finalname = finalname.Substring(0, finalname.Length - 1) + "Collection";
                }
                var xamltype = XamlTypeUtils.GetXamlType(finalname);

                if (xamltype == null)
                {
                    throw new Exception($"type {xamltype} not found");
                }
                if (typeOfChildrenCollectionItems.IsAssignableFrom(xamltype))
                {
                    return(true);
                }
                return(false);
            }
コード例 #2
0
            private string AsTypeOfProperty(string element, string propertyName, string value)
            {
                string attributeInitializerLValue = $"_this.{GetLastSegment(propertyName)}";

                if (value[0] != '"' || value[value.Length - 1] != '"')
                {
                    // the value is not a literal. Assume that things are correctly type,
                    // worst-case the C# compiler will complain
                    return($"{attributeInitializerLValue} = {value};");
                }
                else if (propertyName == "Id")
                {
                    return(value); // this is a fake property, we'll figure x:ref stuff later
                }
                string rawValue = value.Substring(1, value.Length - 2);
                var    type     = XamlTypeUtils.GetXamlType(element);

                if (type == null)
                {
                    throw new Exception($"Xaml type {element} not found");
                }
                var propinfo = type.GetProperty(propertyName);

                if (propinfo == null)
                {
                    // this isn't a property on the type. Maybe it's an attached property
                    if (propertyName.IndexOf('.') != -1)
                    {
                        element      = propertyName.Substring(0, propertyName.LastIndexOf('.'));
                        propertyName = GetLastSegment(propertyName);
                        type         = XamlTypeUtils.GetXamlType(element);
                        if (type == null)
                        {
                            throw new Exception($"Attribute {propertyName} looks like an attached property but {element} isn't a recognized type");
                        }
                        else
                        {
                            var setter = type.GetMethod($"Set{propertyName}");
                            // The syntax for attached properties is:
                            // theParentElement.SetFoo(theCurrentElement, theValue);
                            if (setter != null && setter.IsStatic && setter.GetParameters().Length == 2)
                            {
                                var typeOfParameter = setter.GetParameters() [1].ParameterType;
                                return($"{element}.{setter.Name}(_this, {AsType(rawValue, typeOfParameter, propertyName)});");
                            }
                        }
                    }
                    throw new Exception($"Property {propertyName} not found in type {element}");
                }
                var proptype = propinfo.PropertyType;

                return($"{attributeInitializerLValue} = {AsType(rawValue, proptype, element)};");
            }
コード例 #3
0
            private string GetHandlerParams(string propertyName, string type)
            {
                propertyName = GetLastSegment(propertyName);
                Type xamltype = XamlTypeUtils.GetXamlType(type);

                if (xamltype == null)
                {
                    throw new Exception($"Xaml type {type} not found");
                }
                var eventinfo = xamltype.GetEvent(propertyName);

                if (eventinfo == null)
                {
                    throw new Exception($"Event handler {propertyName} not found in type {type}");
                }
                var eventparams = eventinfo.EventHandlerType.GetMethod("Invoke")?.GetParameters();

                if (eventparams == null)
                {
                    throw new Exception($"Couldn't determine event handler parameter types for event {propertyName} in type {type}");
                }
                return(string.Join(", ", eventparams.Select((p) => p.Name)));
            }