public CreateValuesVisitor(HydrationContext context) { Context = context; }
public FillResourceDictionariesVisitor(HydrationContext context) { Context = context; }
static bool TryAddToProperty(object element, XmlName propertyName, object value, string xKey, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, HydrationContext context, out Exception exception) { exception = null; object targetProperty; var collection = GetPropertyValue(element, propertyName, context, lineInfo, out targetProperty) as IEnumerable; if (collection == null) { return(false); } if (exception == null && TryAddToResourceDictionary(collection as ResourceDictionary, value, xKey, lineInfo, out exception)) { return(true); } if (exception != null) { return(false); } var addMethod = collection.GetType().GetRuntimeMethods().First(mi => mi.Name == "Add" && mi.GetParameters().Length == 1); if (addMethod == null) { return(false); } if (serviceProvider != null && serviceProvider.IProvideValueTarget != null) { ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = targetProperty; } addMethod.Invoke(collection, new [] { value.ConvertTo(addMethod.GetParameters() [0].ParameterType, (Func <TypeConverter>)null, serviceProvider) }); return(true); }
public NamescopingVisitor(HydrationContext context) { Values = context.Values; }
static bool TrySetProperty(object element, string localName, object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, HydrationContext context, out Exception exception) { exception = null; var elementType = element.GetType(); var propertyInfo = elementType.GetRuntimeProperties().FirstOrDefault(p => p.Name == localName); MethodInfo setter; if (propertyInfo == null || !propertyInfo.CanWrite || (setter = propertyInfo.SetMethod) == null) { return(false); } if (!IsVisibleFrom(setter, context.RootElement)) { return(false); } if (serviceProvider != null && serviceProvider.IProvideValueTarget != null) { ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = propertyInfo; } object convertedValue = value.ConvertTo(propertyInfo.PropertyType, () => propertyInfo, serviceProvider); if (convertedValue != null && !propertyInfo.PropertyType.IsInstanceOfType(convertedValue)) { return(false); } setter.Invoke(element, new object [] { convertedValue }); return(true); }
static bool TryGetProperty(object element, string localName, out object value, IXmlLineInfo lineInfo, HydrationContext context, out Exception exception, out object targetProperty) { exception = null; value = null; var elementType = element.GetType(); PropertyInfo propertyInfo = null; try { propertyInfo = elementType.GetRuntimeProperty(localName); } catch (AmbiguousMatchException) { // Get most derived instance of property foreach (var property in elementType.GetRuntimeProperties().Where(prop => prop.Name == localName)) { if (propertyInfo == null || propertyInfo.DeclaringType.IsAssignableFrom(property.DeclaringType)) { propertyInfo = property; } } } MethodInfo getter; targetProperty = propertyInfo; if (propertyInfo == null || !propertyInfo.CanRead || (getter = propertyInfo.GetMethod) == null) { return(false); } if (!IsVisibleFrom(getter, context.RootElement)) { return(false); } value = getter.Invoke(element, new object[] { }); return(true); }
public static void SetPropertyValue(object xamlelement, XmlName propertyName, object value, object rootElement, INode node, HydrationContext context, IXmlLineInfo lineInfo) { var localName = propertyName.LocalName; var serviceProvider = new XamlServiceProvider(node, context); Exception xpe = null; var xKey = node is IElementNode && ((IElementNode)node).Properties.ContainsKey(XmlName.xKey) ? ((ValueNode)((IElementNode)node).Properties[XmlName.xKey]).Value as string : null; //If it's an attached BP, update elementType and propertyName var bpOwnerType = xamlelement.GetType(); var attached = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo); var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false); //If the target is an event, connect if (xpe == null && TryConnectEvent(xamlelement, localName, attached, value, rootElement, lineInfo, out xpe)) { return; } //If Value is DynamicResource and it's a BP, SetDynamicResource if (xpe == null && TrySetDynamicResource(xamlelement, property, value, lineInfo, out xpe)) { return; } //If value is BindingBase, SetBinding if (xpe == null && TrySetBinding(xamlelement, property, localName, value, lineInfo, out xpe)) { return; } //If it's a BindableProberty, SetValue if (xpe == null && TrySetValue(xamlelement, property, attached, value, lineInfo, serviceProvider, out xpe)) { return; } //If we can assign that value to a normal property, let's do it if (xpe == null && TrySetProperty(xamlelement, localName, value, lineInfo, serviceProvider, context, out xpe)) { return; } //If it's an already initialized property, add to it if (xpe == null && TryAddToProperty(xamlelement, propertyName, value, xKey, lineInfo, serviceProvider, context, out xpe)) { return; } xpe = xpe ?? new XamlParseException($"Cannot assign property \"{localName}\": Property does not exist, or is not assignable, or mismatching type between value and property", lineInfo); if (context.ExceptionHandler != null) { context.ExceptionHandler(xpe); } else { throw xpe; } }
public static object GetPropertyValue(object xamlElement, XmlName propertyName, HydrationContext context, IXmlLineInfo lineInfo, out object targetProperty) { var localName = propertyName.LocalName; Exception xpe = null; object value; targetProperty = null; //If it's an attached BP, update elementType and propertyName var bpOwnerType = xamlElement.GetType(); var attached = GetRealNameAndType(ref bpOwnerType, propertyName.NamespaceURI, ref localName, context, lineInfo); var property = GetBindableProperty(bpOwnerType, localName, lineInfo, false); //If it's a BindableProberty, GetValue if (xpe == null && TryGetValue(xamlElement, property, attached, out value, lineInfo, out xpe, out targetProperty)) { return(value); } //If it's a normal property, get it if (xpe == null && TryGetProperty(xamlElement, localName, out value, lineInfo, context, out xpe, out targetProperty)) { return(value); } xpe = xpe ?? new XamlParseException($"Property {localName} is not found or does not have an accessible getter", lineInfo); if (context.ExceptionHandler != null) { context.ExceptionHandler(xpe); } else { throw xpe; } return(null); }
public ApplyPropertiesVisitor(HydrationContext context, bool stopOnResourceDictionary = false) { Context = context; StopOnResourceDictionary = stopOnResourceDictionary; }
public ExpandMarkupsVisitor(HydrationContext context) { Context = context; }
public static object Create(XmlReader reader, bool doNotThrow = false) { object inflatedView = null; if (reader != null) { while (reader.Read()) { //Skip until element if (reader.NodeType == XmlNodeType.Whitespace) { continue; } if (reader.NodeType == XmlNodeType.XmlDeclaration) { continue; } if (reader.NodeType != XmlNodeType.Element) { Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value); continue; } var rootnode = new RuntimeRootNode(new XmlType(reader.NamespaceURI, reader.Name, null), null, (IXmlNamespaceResolver)reader); XamlParser.ParseXaml(rootnode, reader); var visitorContext = new HydrationContext { ExceptionHandler = doNotThrow ? e => { } : (Action <Exception>)null, }; var cvv = new CreateValuesVisitor(visitorContext); // Visit Parameter Properties to create instance from parameterized constructor var type = XamlParser.GetElementType(rootnode.XmlType, rootnode, null, out XamlParseException xpe); if (xpe != null) { throw xpe; } var ctorInfo = type.GetTypeInfo() .DeclaredConstructors.FirstOrDefault( ci => ci.GetParameters().Length != 0 && ci.IsPublic && ci.GetParameters().All(pi => pi.CustomAttributes.Any(attr => attr.AttributeType == typeof(ParameterAttribute)))); if (ctorInfo != null) { foreach (var parameter in ctorInfo.GetParameters()) { var propname = parameter.CustomAttributes.First(ca => ca.AttributeType.FullName == "Tizen.NUI.Binding.ParameterAttribute")? .ConstructorArguments.First() .Value as string; var name = new XmlName("", propname); if (rootnode.Properties.TryGetValue(name, out INode node) && node is ValueNode) { node.Accept(cvv, rootnode); } } } cvv.Visit((ElementNode)rootnode, null); inflatedView = rootnode.Root = visitorContext.Values[rootnode]; visitorContext.RootElement = inflatedView as BindableObject; Visit(rootnode, visitorContext); break; } } return(inflatedView); }
static bool TrySetProperty(object element, string localName, object value, IXmlLineInfo lineInfo, XamlServiceProvider serviceProvider, HydrationContext context, out Exception exception) { exception = null; var elementType = element.GetType(); var propertyInfo = elementType.GetRuntimeProperties().FirstOrDefault(p => p.Name == localName); MethodInfo setter; if (propertyInfo == null || !propertyInfo.CanWrite || (setter = propertyInfo.SetMethod) == null) { return(false); } if (!IsVisibleFrom(setter, context.RootElement)) { return(false); } if (serviceProvider != null && serviceProvider.IProvideValueTarget != null) { ((XamlValueTargetProvider)serviceProvider.IProvideValueTarget).TargetProperty = propertyInfo; } object convertedValue = GetConvertedValue(propertyInfo.PropertyType, value, () => propertyInfo, serviceProvider); if (null == convertedValue) { var methods = propertyInfo.PropertyType.GetMethods().Where(a => a.Name == "op_Implicit"); foreach (var method in methods) { var paramType = method.GetParameters()[0].ParameterType; convertedValue = GetConvertedValue(paramType, value, () => propertyInfo, serviceProvider); if (null != convertedValue) { var realValue = Activator.CreateInstance(propertyInfo.PropertyType); convertedValue = method.Invoke(realValue, new object[] { convertedValue }); if (null != convertedValue) { break; } } } } if (null == convertedValue) { return(false); } setter.Invoke(element, new object[] { convertedValue }); return(true); }
public RegisterXNamesVisitor(HydrationContext context) { Values = context.Values; }