// We don't cache NameScopeProperty because it's only used at the root. // But we have the lookup logic here so that we don't need to do reflection in ObjectWriter. internal static XamlMember LookupNameScopeProperty(XamlType xamlType) { if (xamlType.UnderlyingType == null) { return(null); } // We only check this once, at the root of the doc, and only in ObjectWriter. // So it's fine to use live reflection here. object obj = GetCustomAttribute(typeof(XAML3.NameScopePropertyAttribute), xamlType.UnderlyingType); XAML3.NameScopePropertyAttribute nspAttr = obj as XAML3.NameScopePropertyAttribute; if (nspAttr != null) { Type ownerType = nspAttr.Type; string propertyName = nspAttr.Name; XamlMember prop; if (ownerType != null) { XamlType ownerXamlType = xamlType.SchemaContext.GetXamlType(ownerType); prop = ownerXamlType.GetAttachableMember(propertyName); } else { prop = xamlType.GetMember(propertyName); } return(prop); } return(null); }
public void WriteAttachableEvent() { XamlSchemaContext xsc = new XamlSchemaContext(); XamlObjectWriter ow = new XamlObjectWriter(xsc); XamlType eventElementXT = xsc.GetXamlType(typeof(EventElement)); XamlType eventHolderXT = xsc.GetXamlType(typeof(AttachedEventHolder)); XamlType delegateCreatingMeXT = xsc.GetXamlType(typeof(DelegateCreatingME)); ow.WriteStartObject(eventElementXT); ow.WriteStartMember(eventHolderXT.GetAttachableMember("TapEvent")); ow.WriteStartObject(delegateCreatingMeXT); ow.WriteEndObject(); ow.WriteEndMember(); ow.WriteEndObject(); EventElement eventElement = ((EventElement)ow.Result); int fireCount = eventElement.TapEventCount; AttachedEventHolder.RaiseTapEvent(eventElement); if (eventElement.TapEventCount != fireCount + 1) { throw new Exception("Tap event did not fire"); } }
public bool ProcessNode(XamlReader reader, XamlWriter targetWriter, int currentDepth, IXamlLineInfo readerLineInfo) { if ((currentDepth == this.Depth) && ((reader.NodeType == XamlNodeType.NamespaceDeclaration) || (reader.NodeType == XamlNodeType.None))) { this.bufferedNodes.Writer.WriteNode(reader, readerLineInfo); reader.Read(); return(true); } if ((((reader.NodeType == XamlNodeType.StartObject) && reader.Type.IsGeneric) && ((reader.Type.UnderlyingType != null) && (reader.Type.Name == "PropertyReferenceExtension"))) && (reader.Type.UnderlyingType.GetGenericTypeDefinition() == typeof(PropertyReferenceExtension <>))) { if (this.bufferedNodes.Count > 0) { XamlServices.Transform(this.bufferedNodes.Reader, targetWriter, false); this.bufferedNodes = null; } XamlType type = reader.Type; XamlReader reader2 = reader.ReadSubtree(); XamlType xamlType = reader.SchemaContext.GetXamlType(typeof(ActivityBuilder)); XamlType type3 = reader.SchemaContext.GetXamlType(typeof(ActivityPropertyReference)); targetWriter.WriteStartMember(xamlType.GetAttachableMember("PropertyReference"), readerLineInfo); reader2.Read(); targetWriter.WriteStartObject(type3, readerLineInfo); targetWriter.WriteStartMember(type3.GetMember("TargetProperty"), readerLineInfo); targetWriter.WriteValue(this.currentMember.Name, readerLineInfo); targetWriter.WriteEndMember(readerLineInfo); bool flag = reader2.Read(); bool flag2 = false; while (flag) { if (((reader2.NodeType == XamlNodeType.StartMember) && (reader2.Member.DeclaringType == type)) && (reader2.Member.Name == "PropertyName")) { flag2 = true; } else if (flag2) { if (reader2.NodeType == XamlNodeType.EndMember) { flag2 = false; } else if (reader2.NodeType == XamlNodeType.Value) { targetWriter.WriteStartMember(type3.GetMember("SourceProperty"), readerLineInfo); targetWriter.WriteValue((string)reader2.Value, readerLineInfo); targetWriter.WriteEndMember(readerLineInfo); } } flag = reader2.Read(); } targetWriter.WriteEndObject(readerLineInfo); targetWriter.WriteEndMember(readerLineInfo); this.ExitObject = true; reader2.Close(); } else { this.FlushBuffer(targetWriter); targetWriter.WriteNode(reader, readerLineInfo); } return(false); }
// XamlDomObject.GetMemberNode() needs to take a XamlType and string for supporting unknown attached members. // This constructor is here to match that. public XamlDomMember(XamlType declaringType, string attachedMember, params XamlDomItem[] items) { if (declaringType == null) { throw new ArgumentNullException("declaringType"); } if (attachedMember == null) { throw new ArgumentNullException("attachedMember"); } foreach (var itemNode in items) { Items.Add(itemNode); } var member = declaringType.GetAttachableMember(attachedMember); if (member == null) { throw new NotImplementedException("Unknown members are not implemented"); } _member = member; Resolve(); }
public XamlMember GetXamlAttachableProperty(XamlType xamlType, string propertyName) { if (xamlType.IsUnknown) { return(null); } XamlMember member = xamlType.GetAttachableMember(propertyName); return(IsVisible(member, null) ? member : null); }
public bool HasAttachableMember(XamlType declaringType, string attachableMember) { if (attachableMember == null) { throw new ArgumentNullException("attachableMember"); } // TODO: Handle unknowns return(HasMember(declaringType.GetAttachableMember(attachableMember))); }
private void ValidateUnknown(XamlMember member) { if (member == XamlLanguage.UnknownContent) { ValidationError(SR.MemberUnknownContect(GetXamlTypeName(_stack.TopFrame.Type))); } else if (member.IsUnknown) { bool retryAttachable = false; XamlType declaringType = member.DeclaringType; if (_stack.Depth == 1 && declaringType.IsUnknown && !string.IsNullOrEmpty(this.rootNamespace) && this.definedType != null && declaringType.Name == this.definedType.Name) { // Need to handle the case where the namespace of a member on the document root // is missing the project root namespace string clrNs; if (XamlBuildTaskServices.TryExtractClrNs(declaringType.PreferredXamlNamespace, out clrNs)) { clrNs = string.IsNullOrEmpty(clrNs) ? this.rootNamespace : this.rootNamespace + "." + clrNs; if (clrNs == this.definedType.Namespace) { declaringType = SchemaContext.GetXamlType(this.definedType); retryAttachable = true; } } } XamlMember typeMember = declaringType.GetMember(member.Name); if (typeMember == null && retryAttachable) { typeMember = declaringType.GetAttachableMember(member.Name); } if (typeMember == null || typeMember.IsUnknown) { if (member.IsAttachable) { ValidationError(SR.UnresolvedAttachableMember(GetXamlTypeName(member.DeclaringType) + "." + member.Name)); } else if (member.IsDirective) { ValidationError(SR.UnresolvedDirective(member.PreferredXamlNamespace + ":" + member.Name)); } else { // Skip if declaring type is unknown as the member unknown error messages become redundant. if (declaringType != null && !declaringType.IsUnknown) { ValidationError(SR.UnresolvedMember(member.Name, GetXamlTypeName(declaringType))); } } } } }
public XamlMember GetXamlAttachableProperty(XamlType xamlType, string propertyName) { if (xamlType.IsUnknown) { return(null); } XamlMember attachableMember = xamlType.GetAttachableMember(propertyName); if (!this.IsVisible(attachableMember, null)) { return(null); } return(attachableMember); }
internal XamlMember GetProperty(Int16 propertyId, XamlType parentType) { BamlProperty bamlProperty; XamlMember xamlMember; if (TryGetBamlProperty(propertyId, out bamlProperty, out xamlMember)) { if (xamlMember != null) { return(xamlMember); } XamlType declaringType = GetXamlType(bamlProperty.DeclaringTypeId); // If the parent type & declaring type are assignable, then it could // be either a regular property or attachable. if (parentType.CanAssignTo(declaringType)) { xamlMember = declaringType.GetMember(bamlProperty.Name); if (xamlMember == null) { xamlMember = declaringType.GetAttachableMember(bamlProperty.Name); } } else { //If not assignable, it has to be an attachable member xamlMember = declaringType.GetAttachableMember(bamlProperty.Name); } lock (_syncObject) { _bamlProperty[propertyId] = xamlMember; } return(xamlMember); } throw new KeyNotFoundException(); }
private XamlMember ResolveXamlMember(XamlType declaringType, string member) { if (declaringType != null) { return(declaringType.GetAttachableMember(member)); } if (!IsGetObject) { return(Type.GetMember(member)); } if (Parent != null) { Parent.Member.Type.GetMember(member); } return(null); }
public void AttachedPropertyTest() { var generated = new StringBuilder(); var xmlSettings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }; var xamlSettings = new XamlXmlWriterSettings { CloseOutput = true }; var xsc = new XamlSchemaContext(); using (System.Xaml.XamlXmlWriter writer = new System.Xaml.XamlXmlWriter(XmlWriter.Create(new StringWriter(generated), xmlSettings), xsc, xamlSettings)) { XamlType mc = xsc.GetXamlType(typeof(MediumContainer)); XamlMember thing = mc.GetAttachableMember("Thing"); var developerType = xsc.GetXamlType(typeof(Developer)); var developerExtensionType = xsc.GetXamlType(typeof(DeveloperExtension)); string ns = "clr-namespace:Test.Elements.AnotherNamespace;assembly=XamlTestClasses"; writer.WriteNamespace(new NamespaceDeclaration(ns, "PreferredPrefix")); writer.WriteStartObject(developerType); writer.WriteStartMember(developerType.GetMember("Name")); writer.WriteValue("Andrew"); writer.WriteEndMember(); writer.WriteStartMember(developerType.GetMember("Friend")); writer.WriteStartObject(developerExtensionType); writer.WriteStartMember(developerExtensionType.GetMember("Name")); writer.WriteValue(""); writer.WriteEndMember(); writer.WriteEndObject(); writer.WriteEndMember(); writer.WriteStartMember(thing); writer.WriteValue("attached"); writer.WriteEndMember(); writer.WriteEndObject(); } var target = @"<Developer Name=""Andrew"" Friend=""{Developer Name=""}"" PreferredPrefix:MediumContainer.Thing=""attached"" xmlns=""clr-namespace:Test.Elements;assembly=XamlTestClasses"" xmlns:PreferredPrefix=""clr-namespace:Test.Elements.AnotherNamespace;assembly=XamlTestClasses"" />"; Assert.AreEqual(target, generated.ToString()); }
// member element, implicit member, children via content property, or value IEnumerable<XamlXmlNodeInfo> ReadMemberElement (XamlType parentType, XamlType xt) { XamlMember xm = null; var name = r.LocalName; int idx = name.IndexOf ('.'); // FIXME: it skips strict type name check, as it could result in MarkupExtension mismatch (could be still checked, though) if (idx >= 0/* && name.Substring (0, idx) == xt.Name*/) { name = name.Substring (idx + 1); xm = xt.GetMember (name); } else { xm = (XamlMember) FindStandardDirective (name, AllowedMemberLocations.MemberElement) ?? // not a standard directive? then try attachable xt.GetAttachableMember (name) ?? // still not? then try ordinal member xt.GetMember (name); if (xm == null) { // still not? could it be omitted as content property or items ? if ((xm = GetExtraMember (xt)) != null) { // Note that this does not involve r.Read() foreach (var ni in ReadMember (xt, xm)) yield return ni; yield break; } } } if (xm == null) { // Current element could be for another member in the parent type (if exists) if (parentType != null && parentType.GetMember (name) != null) { // stop the iteration and signal the caller to not read current element as an object. (It resolves conflicts between "start object for current collection's item" and "start member for the next member in the parent object". yield return Node (XamlNodeType.None, null); yield break; } // ok, then create unknown member. xm = new XamlMember (name, xt, false); // FIXME: not sure if isAttachable is always false. } if (!r.IsEmptyElement) { r.Read (); foreach (var ni in ReadMember (xt, xm)) yield return ni; r.MoveToContent (); r.ReadEndElement (); } else r.Read (); }
protected void Read_AttachedProperty (XamlReader r) { var at = new XamlType (typeof (Attachable), r.SchemaContext); Assert.IsTrue (r.Read (), "ns#1-1"); Assert.AreEqual (XamlNodeType.NamespaceDeclaration, r.NodeType, "ns#1-2"); Assert.IsNotNull (r.Namespace, "ns#1-3"); Assert.AreEqual ("", r.Namespace.Prefix, "ns#1-4"); var assns = "clr-namespace:MonoTests.System.Xaml;assembly=" + GetType ().Assembly.GetName ().Name; Assert.AreEqual (assns, r.Namespace.Namespace, "ns#1-5"); // t:AttachedWrapper Assert.IsTrue (r.Read (), "so#1-1"); Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "so#1-2"); var xt = new XamlType (typeof (AttachedWrapper), r.SchemaContext); Assert.AreEqual (xt, r.Type, "so#1-3"); if (r is XamlXmlReader) ReadBase (r); ReadAttachedProperty (r, at.GetAttachableMember ("Foo"), "x", "x"); // m:Value Assert.IsTrue (r.Read (), "sm#2-1"); Assert.AreEqual (XamlNodeType.StartMember, r.NodeType, "#sm#2-2"); Assert.AreEqual (xt.GetMember ("Value"), r.Member, "sm#2-3"); // t:Attached Assert.IsTrue (r.Read (), "so#2-1"); Assert.AreEqual (XamlNodeType.StartObject, r.NodeType, "#so#2-2"); Assert.AreEqual (r.SchemaContext.GetXamlType (typeof (Attached)), r.Type, "so#2-3"); ReadAttachedProperty (r, at.GetAttachableMember ("Foo"), "y", "y"); Assert.IsTrue (r.Read (), "eo#2-1"); Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "#eo#2-2"); // /m:Value Assert.IsTrue (r.Read (), "em#2-1"); Assert.AreEqual (XamlNodeType.EndMember, r.NodeType, "#em#2-2"); // /t:AttachedWrapper Assert.IsTrue (r.Read (), "eo#1-1"); Assert.AreEqual (XamlNodeType.EndObject, r.NodeType, "eo#1-2"); Assert.IsFalse (r.Read (), "end"); }
// XamlDomObject.GetMemberNode() needs to take a XamlType and string for supporting unknown attached members. // This constructor is here to match that. public XamlDomMember(XamlType declaringType, string attachedMember, params XamlDomItem[] items) { if (declaringType == null) { throw new ArgumentNullException("declaringType"); } if (attachedMember == null) { throw new ArgumentNullException("attachedMember"); } foreach (var itemNode in items) { Items.Add(itemNode); } var member = declaringType.GetAttachableMember(attachedMember); if (member == null) { throw new NotImplementedException("Unknown members are not implemented"); } _member = member; Resolve(); }
public XamlMember GetAttachableMember(XamlType xamlType, string name) { return xamlType.GetAttachableMember(name); }
public XamlMember GetXamlAttachableProperty(XamlType xamlType, string propertyName) { if (xamlType.IsUnknown) { return null; } XamlMember attachableMember = xamlType.GetAttachableMember(propertyName); if (!this.IsVisible(attachableMember, null)) { return null; } return attachableMember; }