From 3b8170b02746dfc718c13e324730ed6e83fac5ad Mon Sep 17 00:00:00 2001 From: Iain McCoy Date: Fri, 8 Jul 2005 12:32:27 +0000 Subject: 2005-07-06 Iain McCoy * demo/test.xaml: add use of x:Name attribute * Mono.Windows.Serialization/XamlParser.cs, Mono.Windows.Serialization/XamlWriter.cs, Mono.Windows.Serialization/CodeWriter.cs: support for x:Name attributes and for considering Name properties to have the same affect. svn path=/trunk/mcs/; revision=47091 --- mcs/class/PresentationFramework/ChangeLog | 8 +++++ .../Mono.Windows.Serialization/CodeWriter.cs | 35 +++++++++++++++------- .../Mono.Windows.Serialization/XamlParser.cs | 21 ++++++------- .../Mono.Windows.Serialization/XamlWriter.cs | 2 +- 4 files changed, 44 insertions(+), 22 deletions(-) (limited to 'mcs/class/PresentationFramework') diff --git a/mcs/class/PresentationFramework/ChangeLog b/mcs/class/PresentationFramework/ChangeLog index 21baffd7d49..32741738996 100644 --- a/mcs/class/PresentationFramework/ChangeLog +++ b/mcs/class/PresentationFramework/ChangeLog @@ -1,3 +1,11 @@ +2005-07-08 Iain McCoy + + * Mono.Windows.Serialization/XamlParser.cs, + Mono.Windows.Serialization/XamlWriter.cs, + Mono.Windows.Serialization/CodeWriter.cs: support for x:Name + attributes and for considering Name properties to have the same + affect. + 2005-07-06 Iain McCoy * Mono.Windows.Serialization/CodeWriter.cs: cleaned up a little, added diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs index 8ac2792deca..5ed0c3118fb 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs @@ -89,13 +89,19 @@ namespace Mono.Windows.Serialization { // bottom of stack holds CodeVariableReferenceExpression // pushes a reference to the new current type - public void CreateObject(Type type) + public void CreateObject(Type type, string varName) { - string varName = Char.ToLower(type.Name[0]) + type.Name.Substring(1); - // make sure something sensible happens when class - // names start with a lowercase letter - if (varName == type.Name) - varName = "_" + varName; + bool isDefaultName; + if (varName == null) { + isDefaultName = true; + varName = Char.ToLower(type.Name[0]) + type.Name.Substring(1); + // make sure something sensible happens when class + // names start with a lowercase letter + if (varName == type.Name) + varName = "_" + varName; + } else { + isDefaultName = false; + } if (!nameClashes.ContainsKey(varName)) nameClashes[varName] = 0; @@ -104,16 +110,23 @@ namespace Mono.Windows.Serialization { varName += (int)nameClashes[varName]; } - CodeVariableDeclarationStatement declaration = - new CodeVariableDeclarationStatement(type, - varName, - new CodeObjectCreateExpression(type)); + + if (isDefaultName) { + CodeVariableDeclarationStatement declaration = + new CodeVariableDeclarationStatement(type, + varName, + new CodeObjectCreateExpression(type)); + constructor.Statements.Add(declaration); + } else { + CodeMemberField declaration = new CodeMemberField(type, varName); + declaration.InitExpression = new CodeObjectCreateExpression(type); + this.type.Members.Add(declaration); + } CodeVariableReferenceExpression varRef = new CodeVariableReferenceExpression(varName); CodeMethodInvokeExpression addChild = new CodeMethodInvokeExpression( (CodeExpression)objects[objects.Count - 1], "AddChild", varRef); - constructor.Statements.Add(declaration); constructor.Statements.Add(addChild); objects.Add(varRef); } diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs index 749f9feefb7..369c6532fea 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs @@ -242,17 +242,18 @@ namespace Mono.Windows.Serialization { void parseObjectElement() { Type parent; - string objectName = null; bool isEmpty = reader.IsEmptyElement; parent = mapper.GetType(reader.NamespaceURI, reader.Name); - objectName = reader.GetAttribute("Class", XAML_NAMESPACE); if (parent.GetInterface("System.Windows.Serialization.IAddChild") == null) {} //TODO: throw exception if (currentState == null) { - createTopLevel(parent.AssemblyQualifiedName, objectName); + createTopLevel(parent.AssemblyQualifiedName, reader.GetAttribute("Class", XAML_NAMESPACE)); } else { - addChild(parent); + string name = reader.GetAttribute("Name", XAML_NAMESPACE); + if (name == null) + name = reader.GetAttribute("Name", reader.NamespaceURI); + addChild(parent, name); } if (reader.MoveToFirstAttribute()) { @@ -275,21 +276,21 @@ namespace Mono.Windows.Serialization { } } - void createTopLevel(string parentName, string objectName) + void createTopLevel(string parentName, string className) { Type t = Type.GetType(parentName); currentState = new ParserState(); currentState.type = CurrentType.Object; currentState.obj = t; - if (objectName == null) { - objectName = "derived" + t.Name; + if (className == null) { + className = "derived" + t.Name; } - writer.CreateTopLevel(t, objectName); + writer.CreateTopLevel(t, className); } - void addChild(Type type) + void addChild(Type type, string objectName) { - writer.CreateObject(type); + writer.CreateObject(type, objectName); oldStates.Add(currentState); currentState = new ParserState(); currentState.type = CurrentType.Object; diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlWriter.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlWriter.cs index 0c93c74c955..38d7170c94a 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlWriter.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlWriter.cs @@ -33,7 +33,7 @@ namespace Mono.Windows.Serialization { public interface XamlWriter { void CreateTopLevel(Type parent, string className); - void CreateObject(Type type); + void CreateObject(Type type, string name); void CreateElementText(string text); void EndObject(); -- cgit v1.2.3