public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { var valueNode = ((IElementNode)node).Properties[new XmlName("", "Value")]; //if it's an elementNode, there's probably no need to convert it if (valueNode is IElementNode) { yield break; } var value = ((string)((ValueNode)valueNode).Value); var bpNode = ((ValueNode)((IElementNode)node).Properties[new XmlName("", "Property")]); var bpRef = (new BindablePropertyConverter()).GetBindablePropertyFieldReference((string)bpNode.Value, module, bpNode); TypeReference _; var setValueRef = module.Import(module.Import(typeof(Setter)).GetProperty(p => p.Name == "Value", out _).SetMethod); //push the setter yield return(Instruction.Create(OpCodes.Ldloc, vardefref.VariableDefinition)); //push the value foreach (var instruction in ((ValueNode)valueNode).PushConvertedValue(context, bpRef, valueNode.PushServiceProvider(context, bpRef: bpRef), boxValueTypes: true, unboxValueTypes: false)) { yield return(instruction); } //set the value yield return(Instruction.Create(OpCodes.Callvirt, setValueRef)); }
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { INode valueNode = null; if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Value"), out valueNode) && !((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Value"), out valueNode) && ((IElementNode)node).CollectionItems.Count == 1) { valueNode = ((IElementNode)node).CollectionItems[0]; } var bpNode = ((ValueNode)((IElementNode)node).Properties[new XmlName("", "Property")]); var bpRef = (new BindablePropertyConverter()).GetBindablePropertyFieldReference((string)bpNode.Value, module, bpNode); if (SetterValueIsCollection(bpRef, module, node, context)) { yield break; } if (valueNode == null) { throw new XamlParseException("Missing Value for Setter", (IXmlLineInfo)node); } //if it's an elementNode, there's probably no need to convert it if (valueNode is IElementNode) { yield break; } var value = ((string)((ValueNode)valueNode).Value); var setterType = ("Xamarin.Forms.Core", "Xamarin.Forms", "Setter"); //push the setter foreach (var instruction in vardefref.VariableDefinition.LoadAs(module.GetTypeDefinition(setterType), module)) { yield return(instruction); } //push the value foreach (var instruction in ((ValueNode)valueNode).PushConvertedValue(context, bpRef, valueNode.PushServiceProvider(context, bpRef: bpRef), boxValueTypes: true, unboxValueTypes: false)) { yield return(instruction); } //set the value yield return(Instruction.Create(OpCodes.Callvirt, module.ImportPropertySetterReference(setterType, propertyName: "Value"))); }
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { INode sourceNode = null; ((IElementNode)node).Properties.TryGetValue(new XmlName("", "Source"), out sourceNode); if (sourceNode == null) { ((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Source"), out sourceNode); } INode styleNode = null; if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Style"), out styleNode) && !((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Style"), out styleNode) && ((IElementNode)node).CollectionItems.Count == 1) { styleNode = ((IElementNode)node).CollectionItems[0]; } if (sourceNode != null && styleNode != null) { throw new XamlParseException("StyleSheet can not have both a Source and a content", node); } if (sourceNode == null && styleNode == null) { throw new XamlParseException("StyleSheet require either a Source or a content", node); } if (styleNode != null && !(styleNode is ValueNode)) { throw new XamlParseException("Style property or Content is not a string literal", node); } if (sourceNode != null && !(sourceNode is ValueNode)) { throw new XamlParseException("Source property is not a string literal", node); } if (styleNode != null) { var style = (styleNode as ValueNode).Value as string; yield return(Create(Ldstr, style)); yield return(Create(Call, module.ImportMethodReference(("Xamarin.Forms.Core", "Xamarin.Forms.StyleSheets", "StyleSheet"), methodName: "FromString", parameterTypes: new[] { ("mscorlib", "System", "String") },
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { INode sourceNode = null; ((IElementNode)node).Properties.TryGetValue(new XmlName("", "Source"), out sourceNode); if (sourceNode == null) { ((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Source"), out sourceNode); } INode styleNode = null; if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Style"), out styleNode) && !((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Style"), out styleNode) && ((IElementNode)node).CollectionItems.Count == 1) { styleNode = ((IElementNode)node).CollectionItems[0]; } if (sourceNode != null && styleNode != null) { throw new BuildException(BuildExceptionCode.StyleSheetSourceOrContent, node, null); } if (sourceNode == null && styleNode == null) { throw new BuildException(BuildExceptionCode.StyleSheetNoSourceOrContent, node, null); } if (styleNode != null && !(styleNode is ValueNode)) { throw new BuildException(BuildExceptionCode.StyleSheetStyleNotALiteral, node, null); } if (sourceNode != null && !(sourceNode is ValueNode)) { throw new BuildException(BuildExceptionCode.StyleSheetSourceNotALiteral, node, null); } if (styleNode != null) { var style = (styleNode as ValueNode).Value as string; yield return(Create(Ldstr, style)); yield return(Create(Call, module.ImportMethodReference(("Microsoft.Maui.Controls", "Microsoft.Maui.Controls.StyleSheets", "StyleSheet"), methodName: "FromString", parameterTypes: new[] { ("mscorlib", "System", "String") },
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { INode valueNode = null; if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Value"), out valueNode) && !((IElementNode)node).Properties.TryGetValue(new XmlName("http://xamarin.com/schemas/2014/forms", "Value"), out valueNode) && ((IElementNode)node).CollectionItems.Count == 1) { valueNode = ((IElementNode)node).CollectionItems[0]; } if (valueNode == null) { throw new XamlParseException("Missing Value for Setter", (IXmlLineInfo)node); } //if it's an elementNode, there's probably no need to convert it if (valueNode is IElementNode) { yield break; } var value = ((string)((ValueNode)valueNode).Value); var bpNode = ((ValueNode)((IElementNode)node).Properties[new XmlName("", "Property")]); var bpRef = (new BindablePropertyConverter()).GetBindablePropertyFieldReference((string)bpNode.Value, module, bpNode); TypeReference _; var setValueRef = module.ImportReference(module.ImportReference(typeof(Setter)).GetProperty(p => p.Name == "Value", out _).SetMethod); //push the setter yield return(Instruction.Create(OpCodes.Ldloc, vardefref.VariableDefinition)); //push the value foreach (var instruction in ((ValueNode)valueNode).PushConvertedValue(context, bpRef, valueNode.PushServiceProvider(context, bpRef: bpRef), boxValueTypes: true, unboxValueTypes: false)) { yield return(instruction); } //set the value yield return(Instruction.Create(OpCodes.Callvirt, setValueRef)); }
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { yield break; }
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { INode sourceNode = null; ((IElementNode)node).Properties.TryGetValue(new XmlName("", "Source"), out sourceNode); if (sourceNode == null) { ((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Source"), out sourceNode); } INode styleNode = null; if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Style"), out styleNode) && !((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Style"), out styleNode) && ((IElementNode)node).CollectionItems.Count == 1) { styleNode = ((IElementNode)node).CollectionItems[0]; } if (sourceNode != null && styleNode != null) { throw new XamlParseException("StyleSheet can not have both a Source and a content", node); } if (sourceNode == null && styleNode == null) { throw new XamlParseException("StyleSheet require either a Source or a content", node); } if (styleNode != null && !(styleNode is ValueNode)) { throw new XamlParseException("Style property or Content is not a string literal", node); } if (sourceNode != null && !(sourceNode is ValueNode)) { throw new XamlParseException("Source property is not a string literal", node); } if (styleNode != null) { var style = (styleNode as ValueNode).Value as string; yield return(Create(Ldstr, style)); yield return(Create(Call, module.ImportMethodReference(("Xamarin.Forms.Core", "Xamarin.Forms.StyleSheets", "StyleSheet"), methodName: "FromString", paramCount: 1, predicate: md => md.IsStatic))); } else { var source = (sourceNode as ValueNode)?.Value as string; INode rootNode = node; while (!(rootNode is ILRootNode)) { rootNode = rootNode.Parent; } var rootTargetPath = RDSourceTypeConverter.GetPathForType(module, ((ILRootNode)rootNode).TypeReference); var uri = new Uri(source, UriKind.Relative); var resourcePath = ResourceDictionary.RDSourceTypeConverter.GetResourcePath(uri, rootTargetPath); //fail early var resourceId = XamlCTask.GetResourceIdForPath(module, resourcePath); if (resourceId == null) { throw new XamlParseException($"Resource '{source}' not found.", node); } yield return(Create(Ldtoken, module.ImportReference(((ILRootNode)rootNode).TypeReference))); yield return(Create(Call, module.ImportMethodReference(("mscorlib", "System", "Type"), methodName: "GetTypeFromHandle", paramCount: 1, predicate: md => md.IsStatic))); yield return(Create(Call, module.ImportMethodReference(("mscorlib", "System.Reflection", "IntrospectionExtensions"), methodName: "GetTypeInfo", paramCount: 1, predicate: md => md.IsStatic))); yield return(Create(Callvirt, module.ImportPropertyGetterReference(("mscorlib", "System.Reflection", "TypeInfo"), propertyName: "Assembly", flatten: true))); yield return(Create(Ldstr, resourceId)); //resourceId foreach (var instruction in node.PushXmlLineInfo(context)) { yield return(instruction); //lineinfo } yield return(Create(Call, module.ImportMethodReference(("Xamarin.Forms.Core", "Xamarin.Forms.StyleSheets", "StyleSheet"), methodName: "FromAssemblyResource", paramCount: 3, predicate: md => md.IsStatic))); } //the variable is of type `object`. fix that var vardef = new VariableDefinition(module.ImportReference(("Xamarin.Forms.Core", "Xamarin.Forms.StyleSheets", "StyleSheet"))); yield return(Create(Stloc, vardef)); vardefref.VariableDefinition = vardef; }
public IEnumerable <Instruction> ProvideValue(VariableDefinitionReference vardefref, ModuleDefinition module, BaseNode node, ILContext context) { INode sourceNode = null; ((IElementNode)node).Properties.TryGetValue(new XmlName("", "Source"), out sourceNode); if (sourceNode == null) { ((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Source"), out sourceNode); } INode styleNode = null; if (!((IElementNode)node).Properties.TryGetValue(new XmlName("", "Style"), out styleNode) && !((IElementNode)node).Properties.TryGetValue(new XmlName(XamlParser.XFUri, "Style"), out styleNode) && ((IElementNode)node).CollectionItems.Count == 1) { styleNode = ((IElementNode)node).CollectionItems[0]; } if (sourceNode != null && styleNode != null) { throw new XamlParseException($"StyleSheet can not have both a Source and a content", node); } if (sourceNode == null && styleNode == null) { throw new XamlParseException($"StyleSheet require either a Source or a content", node); } if (styleNode != null && !(styleNode is ValueNode)) { throw new XamlParseException($"Style property or Content is not a string literal", node); } if (sourceNode != null && !(sourceNode is ValueNode)) { throw new XamlParseException($"Source property is not a string literal", node); } if (styleNode != null) { var style = (styleNode as ValueNode).Value as string; yield return(Create(Ldstr, style)); var fromString = module.ImportReferenceCached(typeof(StyleSheets.StyleSheet).GetMethods().FirstOrDefault(mi => mi.Name == nameof(StyleSheets.StyleSheet.FromString) && mi.GetParameters().Length == 1)); yield return(Create(Call, module.ImportReference(fromString))); } else { string source = (sourceNode as ValueNode)?.Value as string; INode rootNode = node; while (!(rootNode is ILRootNode)) { rootNode = rootNode.Parent; } var rootTargetPath = RDSourceTypeConverter.GetPathForType(module, ((ILRootNode)rootNode).TypeReference); var uri = new Uri(source, UriKind.Relative); var resourcePath = ResourceDictionary.RDSourceTypeConverter.GetResourcePath(uri, rootTargetPath); //fail early var resourceId = XamlCTask.GetResourceIdForPath(module, resourcePath); if (resourceId == null) { throw new XamlParseException($"Resource '{source}' not found.", node); } var getTypeFromHandle = module.ImportReferenceCached(typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle), new[] { typeof(RuntimeTypeHandle) })); var getAssembly = module.ImportReferenceCached(typeof(Type).GetProperty(nameof(Type.Assembly)).GetGetMethod()); yield return(Create(Ldtoken, module.ImportReference(((ILRootNode)rootNode).TypeReference))); yield return(Create(Call, module.ImportReference(getTypeFromHandle))); yield return(Create(Callvirt, module.ImportReference(getAssembly))); //assembly yield return(Create(Ldstr, resourceId)); //resourceId foreach (var instruction in node.PushXmlLineInfo(context)) { yield return(instruction); //lineinfo } var fromAssemblyResource = module.ImportReferenceCached(typeof(StyleSheets.StyleSheet).GetMethods().FirstOrDefault(mi => mi.Name == nameof(StyleSheets.StyleSheet.FromAssemblyResource) && mi.GetParameters().Length == 3)); yield return(Create(Call, module.ImportReference(fromAssemblyResource))); } //the variable is of type `object`. fix that var vardef = new VariableDefinition(module.ImportReferenceCached(typeof(StyleSheets.StyleSheet))); yield return(Create(Stloc, vardef)); vardefref.VariableDefinition = vardef; }