From 67874e02b3fbb6483ba50c4be1add4dfdcf6ba70 Mon Sep 17 00:00:00 2001 From: Iain McCoy Date: Tue, 5 Jul 2005 13:23:57 +0000 Subject: 2005-07-05 Iain McCoy * Mono.Windows.Serialization/XamlParser.cs, Mono.Windows.Serialization/XamlWriter.cs, Mono.Windows.Serialization/CodeWriter.cs: add support for attached properties as attributes, supplementing the existing support for attached properties as elements svn path=/trunk/mcs/; revision=46945 --- mcs/class/PresentationFramework/ChangeLog | 8 +++ .../Mono.Windows.Serialization/CodeWriter.cs | 6 +- .../Mono.Windows.Serialization/XamlParser.cs | 64 +++++++++++++++------- 3 files changed, 54 insertions(+), 24 deletions(-) (limited to 'mcs/class/PresentationFramework') diff --git a/mcs/class/PresentationFramework/ChangeLog b/mcs/class/PresentationFramework/ChangeLog index 645b2b023df..c2499135815 100644 --- a/mcs/class/PresentationFramework/ChangeLog +++ b/mcs/class/PresentationFramework/ChangeLog @@ -1,3 +1,11 @@ +2005-07-05 Iain McCoy + + * Mono.Windows.Serialization/XamlParser.cs, + Mono.Windows.Serialization/XamlWriter.cs, + Mono.Windows.Serialization/CodeWriter.cs: add support for attached + properties as attributes, supplementing the existing support for + attached properties as elements + 2005-07-05 Iain McCoy * Mono.Windows.Serialization/XamlParser.cs, diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs index 7f52ea08a13..717aad9795a 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/CodeWriter.cs @@ -132,10 +132,8 @@ namespace Mono.Windows.Serialization { public void CreateAttachedProperty(Type attachedTo, string propertyName, Type propertyType) { string varName = "temp"; - if (tempIndex != 0) { - varName += tempIndex; - tempIndex += 1; - } + varName += tempIndex; + tempIndex += 1; CodeVariableDeclarationStatement decl = new CodeVariableDeclarationStatement(propertyType, varName); constructor.Statements.Add(decl); diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs index a1a9dd62962..fa49d898dd0 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/XamlParser.cs @@ -223,28 +223,13 @@ namespace Mono.Windows.Serialization { } } + void parseAttachedPropertyElement(string attachedTo, string propertyName) { - Type typeAttachedTo = null; - FieldInfo propField; - DependencyProperty dp; Type currentType = (Type)currentState.obj; - if (!currentType.IsSubclassOf(typeof(System.Windows.DependencyObject))) - throw new Exception("Attached properties can only be set on "+ - "DependencyObjects (not " + currentType.Name + ")"); - foreach (ParserState state in oldStates) { - if (state.type == CurrentType.Object && - ((Type)state.obj).Name == attachedTo) { - typeAttachedTo = (Type)state.obj; - break; - } - } - if (typeAttachedTo == null) - throw new Exception("Nothing to attach to: " + attachedTo + "." + propertyName); - propField = typeAttachedTo.GetField(propertyName + "Property"); - if (propField == null) - throw new Exception("Property " + propertyName + " does not exist on " + attachedTo); - dp = (DependencyProperty)propField.GetValue(null); + ensureDependencyObject(currentType); + Type typeAttachedTo = findTypeToAttachTo(attachedTo, propertyName); + DependencyProperty dp = getDependencyProperty(typeAttachedTo, propertyName); oldStates.Add(currentState); currentState = new ParserState(); @@ -346,11 +331,50 @@ namespace Mono.Windows.Serialization { return true; } + void ensureDependencyObject(Type currentType) + { + if (!currentType.IsSubclassOf(typeof(System.Windows.DependencyObject))) + throw new Exception("Attached properties can only be set on "+ + "DependencyObjects (not " + currentType.Name + ")"); + } + Type findTypeToAttachTo(string attachedTo, string propertyName) + { + Type typeAttachedTo = null; + foreach (ParserState state in oldStates) { + if (state.type == CurrentType.Object && + ((Type)state.obj).Name == attachedTo) { + typeAttachedTo = (Type)state.obj; + break; + } + } + if (typeAttachedTo == null) + throw new Exception("Nothing to attach to: " + attachedTo + "." + propertyName); + return typeAttachedTo; + } + DependencyProperty getDependencyProperty(Type typeAttachedTo, string propertyName) + { + FieldInfo propField = typeAttachedTo.GetField(propertyName + "Property"); + if (propField == null) + throw new Exception("Property " + propertyName + " does not exist on " + typeAttachedTo.Name); + return (DependencyProperty)propField.GetValue(null); + } void parseContextPropertyAttribute() { - throw new NotImplementedException("parseContextPropertyAttribute"); + int index = reader.LocalName.LastIndexOf('.'); + string attachedTo = reader.LocalName.Substring(0, index); + string propertyName = reader.LocalName.Substring(index + 1); + + Type currentType = (Type)currentState.obj; + ensureDependencyObject(currentType); + Type typeAttachedTo = findTypeToAttachTo(attachedTo, propertyName); + DependencyProperty dp = getDependencyProperty(typeAttachedTo, propertyName); + + writer.CreateAttachedProperty(typeAttachedTo, propertyName, dp.PropertyType); + writer.CreateAttachedPropertyText(reader.Value, dp.PropertyType, + getTypeConverter(dp.PropertyType)); + writer.EndAttachedProperty(); } void parseEndElement() -- cgit v1.2.3