public static object ConvertObject(XamlParser parser, XamlObjectElement element, Type dest_type, TypeConverter converter, string prop_name, object val) { // Should i return default(T) if property.PropertyType is a valuetype? if (val == null) { return(val); } if (dest_type.IsAssignableFrom(val.GetType())) { return(val); } if (dest_type == typeof(string)) { return(val.ToString()); } if (converter == null || ConverterIsBlackListed(converter)) { converter = new XamlTypeConverter(parser, element, prop_name, dest_type); } return(converter.ConvertFrom(null, Helper.DefaultCulture, val)); }
private void AddChildObject(XamlObjectElement obj) { object value = obj.Object; MutableObject mutable = value as MutableObject; if (mutable != null) { value = mutable.Object; } IList list = Object as IList; if (list != null) { list.Add(value); return; } IDictionary dict = Object as IDictionary; if (dict != null) { dict.Add(obj.GetDictionaryKey(), value); return; } XamlReflectionPropertySetter content_property = FindContentProperty(); if (content_property == null) { throw Parser.ParseException("Unable to add element {0} to element {1}.", obj.Name, Name); } content_property.SetValue(value); }
private static Type GetTargetType(XamlTypeConverter converter) { XamlElement p = converter.parser.CurrentElement.Parent; XamlObjectElement parent = p as XamlObjectElement; if (p == null) { throw new XamlParseException("Attempting to create a DP from an item without a target property."); } if (parent == null) { parent = p.Parent as XamlObjectElement; } if (parent == null) { throw new XamlParseException("Attempting to create a DP from an item without a target property."); } Style s = parent.Object as Style; if (s == null) { throw new XamlParseException("Attempting to create a DP from a non style object."); } return(s.TargetType); }
public static XamlReflectionPropertySetter Create(XamlObjectElement element, object target, Accessors accessors) { if (accessors == null) { return(null); } return(new XamlReflectionPropertySetter(element, target, accessors)); }
public static XamlAttachedPropertySetter Create(XamlObjectElement element, Accessors accessors) { if (accessors == null) { return(null); } return(new XamlAttachedPropertySetter(element, accessors)); }
static Accessors CreateAttachedAccessors(XamlObjectElement parent, Type t, string name) { // See comment in XamlReflectionPropertyForName to see why we special case the case // where there's a DP but no static getter/setter methods. if (string.IsNullOrEmpty(name)) { return(null); } Func <TypeConverter> converter = null; MethodInfo getter = ResolveAttachedPropertyGetter(name, t); MethodInfo setter = ResolveAttachedPropertySetter(name, t, getter == null ? null : getter.ReturnType); if (getter != null) { converter = Helper.GetConverterCreatorFor(getter, getter.ReturnType); } if ((getter == null && setter == null) || (setter == null && !IsCollectionType(getter.ReturnType))) { if (Deployment.Current.MajorVersion >= 4) { // The SL4+ parser will not allow you to set a Binding on DP with no CLR wrapper return(null); } // The old parser will allow you to set a Binding on DP with no CLR wrapper DependencyProperty dp; Types.Ensure(t); if (DependencyProperty.TryLookup(Deployment.Current.Types.TypeToKind(t), name, out dp) && dp.IsAttached) { return(new Accessors( (o) => { throw new XamlParseException(string.Format("The property {0} was not found on element {1}.", name, t.Name)); }, (o, a) => { throw new XamlParseException(string.Format("The property {0} was not found on element {1}.", name, t.Name)); }, dp.PropertyType, dp.Name, converter, dp.DeclaringType )); } else { return(null); } } return(new Accessors( CreateAttachedGetter(getter), CreateAttachedSetter(setter), getter != null ? getter.ReturnType : setter.GetParameters() [1].ParameterType, name, converter, getter != null ? getter.DeclaringType : setter.DeclaringType )); }
XamlReflectionPropertySetter(XamlObjectElement element, object target, Accessors accessors) : base(element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator()) { this.target = target; this.accessors = accessors; if (target is MutableObject) { is_mutable = true; } }
private void AddToCollection(XamlObjectElement obj, object value) { IList list = accessors.Getter(target) as IList; if (list == null) { throw Parser.ParseException("Collection property in non collection type."); } list.Add(value); }
public override void SetValue(XamlObjectElement obj, object value) { MutableObject mutable = value as MutableObject; if (mutable != null) { value = mutable.Object; } Binding binding = value as Binding; if (binding != null && SetBinding(binding, Target)) { return; } if (!typeof(TemplateBindingExpression).IsAssignableFrom(Type)) { TemplateBindingExpression tb = value as TemplateBindingExpression; if (tb != null) { SetTemplateBinding(tb, Target); return; } } // We do this before lists to cover the case where you are setting a list to a list or // a resource dictionary to a resource dictionary, ect // as opposed to adding items to the list or dictionary. // // null is a legal value here because they may have done something like foo="{x:Null}" // if (value == null || Type.IsAssignableFrom(value.GetType())) { accessors.Setter(Target, ConvertValue(Type, value)); return; } if (typeof(IList).IsAssignableFrom(Type)) { AddToCollection(obj, value); return; } if (typeof(IDictionary).IsAssignableFrom(Type)) { AddToDictionary(obj, value); return; } throw Parser.ParseException("Unable to set property {0} to value {1}.", Name, value); }
public override void SetValue(XamlObjectElement obj, object value) { var mutable = value as MutableObject; if (mutable != null) { value = mutable.Object; } if (!typeof(Binding).IsAssignableFrom(Type)) { Binding binding = value as Binding; if (binding != null) { SetBinding(binding, Element.Object); return; } } if (!typeof(TemplateBindingExpression).IsAssignableFrom(Type)) { TemplateBindingExpression tb = value as TemplateBindingExpression; if (tb != null) { SetTemplateBinding(tb, obj.Object); return; } } if (value == null || Type.IsAssignableFrom(value.GetType())) { Accessors.Setter(Element.Object, ConvertValue(Type, value)); return; } if (typeof(IList).IsAssignableFrom(Type)) { AddToCollection(value); return; } throw new XamlParseException( string.Format("XamlAttachedPropertySetter.SetValue: Could not set value '{0}' to the attached property '{1}.{2}'", value, Accessors.DeclaringType, Accessors.Name) ); }
public override void AddChild(XamlElement child) { if (!set_on_add) { return; } XamlObjectElement element = child as XamlObjectElement; if (element == null) { return; } Setter.SetValue(element, element.Object); }
private void AddToDictionary(XamlObjectElement obj, object value) { IDictionary rd = accessors.Getter(target) as IDictionary; if (rd == null) { throw Parser.ParseException("Collection property in non collection type."); } string key = obj.GetDictionaryKey(); if (key == null) { throw Parser.ParseException("You must specify an x:Key or x:Name for elements in a ResourceDictionary"); } rd.Add(key, value); }
public override void SetValue(XamlObjectElement obj, object value) { if (value == null) { throw Parser.ParseException("Setting Name value to null."); } string name = value as string; if (name == null) { throw Parser.ParseException("Unable to set Name property to type {1}.", value.GetType()); } obj.X_Name = name; if (!target.SetNameOnScope(name, Parser.NameScope)) { throw Parser.ParseException("Unable to set Name '{0}' on element '{1}'.", name, Element.Name); } }
public XamlNamePropertySetter (XamlObjectElement element, DependencyObject target) : base (element, "Name", null) { this.target = target; }
public XamlReflectionEventSetter (XamlObjectElement element, object target, EventInfo evnt) : base (element, evnt.Name, Helper.GetConverterFor (evnt, evnt.EventHandlerType)) { this.target = target; this.evnt = evnt; }
private void AddToCollection (XamlObjectElement obj, object value) { IList list = accessors.Getter (target) as IList; if (list == null) { throw Parser.ParseException ("Collection property in non collection type."); } list.Add (value); }
XamlReflectionPropertySetter (XamlObjectElement element, object target, Accessors accessors) : base (element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator ()) { this.target = target; this.accessors = accessors; if (target is MutableObject) is_mutable = true; }
public abstract void SetValue (XamlObjectElement obj, object value);
public override void SetValue(XamlObjectElement obj, object value) { MethodInfo invoker_info = evnt.EventHandlerType.GetMethod("Invoke"); ParameterInfo [] event_params = invoker_info.GetParameters(); string handler_name = value as string; var subscriber = Parser.TopElement; if (subscriber == null) { throw Parser.ParseException("Attempt to set an event handler on an invalid object."); } if (String.IsNullOrEmpty(handler_name)) { throw Parser.ParseException("Attmept to set an event handler to null."); } Delegate d = null; MethodInfo [] methods = subscriber.GetType().GetMethods(XamlParser.EVENT_BINDING_FLAGS); MethodInfo candidate = null; bool name_match = false; for (int i = 0; i < methods.Length; i++) { MethodInfo m = methods [i]; ParameterInfo [] parameters; if (m.Name != handler_name) { continue; } if (name_match) { throw Parser.ParseException("Multiple event handlers found with same name."); } name_match = true; parameters = m.GetParameters(); if (parameters.Length != event_params.Length) { continue; } bool match = true; for (int p = 0; p < parameters.Length; p++) { if (!event_params [p].ParameterType.IsSubclassOf(parameters [p].ParameterType) && parameters [p].ParameterType != event_params [p].ParameterType) { Console.Error.WriteLine("mismatch: {0} and {1}", parameters [p].ParameterType, event_params [p].ParameterType); match = false; break; } } if (!match) { continue; } if (candidate != null) { throw Parser.ParseException("Multiple event handler candidates found for event {0}", Name); } candidate = m; } if (candidate == null) { throw Parser.ParseException("Event handler not found for event {0}.", Name); } d = Delegate.CreateDelegate(evnt.EventHandlerType, subscriber, candidate, false); if (d == null) { throw Parser.ParseException("Unable to create event delegate for event {0}.", Name); } evnt.AddEventHandler(target, d); }
public XamlNamePropertySetter(XamlObjectElement element, DependencyObject target) : base(element, "Name", null) { this.target = target; }
public static XamlAttachedPropertySetter Create (XamlObjectElement element, Accessors accessors) { if (accessors == null) return null; return new XamlAttachedPropertySetter (element, accessors); }
protected XamlPropertySetter(XamlObjectElement element, string name, TypeConverter converter) { Element = element; Name = name; Converter = converter; }
XamlAttachedPropertySetter(XamlObjectElement element, Accessors accessors) : base(element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator()) { Accessors = accessors; }
public override void SetValue (XamlObjectElement obj, object value) { var mutable = value as MutableObject; if (mutable != null) value = mutable.Object; if (!typeof (Binding).IsAssignableFrom (Type)) { Binding binding = value as Binding; if (binding != null) { SetBinding (binding, Element.Object); return; } } if (!typeof (TemplateBindingExpression).IsAssignableFrom (Type)) { TemplateBindingExpression tb = value as TemplateBindingExpression; if (tb != null) { SetTemplateBinding (tb, obj.Object); return; } } if (value == null || Type.IsAssignableFrom (value.GetType ())) { Accessors.Setter (Element.Object, ConvertValue (Type, value)); return; } if (typeof (IList).IsAssignableFrom (Type)) { AddToCollection (value); return; } throw new XamlParseException ( string.Format ("XamlAttachedPropertySetter.SetValue: Could not set value '{0}' to the attached property '{1}.{2}'", value, Accessors.DeclaringType, Accessors.Name) ); }
public XamlTypeConverter(XamlParser parser, XamlObjectElement element, string propertyName, Type destinationType) : base(propertyName, destinationType) { this.parser = parser; this.element = element; }
public override void SetValue (XamlObjectElement obj, object value) { // We do this first to cover the case where you are setting a list to a list or // a resource dictionary to a resource dictionary, binding to a binding, ect // as opposed to adding items to the list or dictionary. if (Type.IsAssignableFrom (value.GetType ())) { prop.SetValue (target, ConvertValue (Type, value), null); return; } { Binding binding = value as Binding; if (binding != null) { SetBinding (binding); return; } } { TemplateBindingExpression tb = value as TemplateBindingExpression; if (tb != null) { SetTemplateBinding (tb); return; } } if (typeof (IList).IsAssignableFrom (Type)) { AddToCollection (obj, value); return; } if (typeof (IDictionary).IsAssignableFrom (Type)) { AddToDictionary (obj, value); return; } throw Parser.ParseException ("Unable to set property {0} to value {1}.", Name, value); }
public XamlAttachedPropertySetter (XamlObjectElement element, string name, MethodInfo getter, MethodInfo setter) : base (element, name, Helper.GetConverterFor (setter, getter.ReturnType)) { this.getter = getter; this.setter = setter; }
public SL4MarkupExpressionParser(object target, string attribute_name, XamlParser parser, XamlObjectElement target_element) : base(target, attribute_name) { this.parser = parser; this.target_element = target_element; }
public XamlReflectionEventSetter(XamlObjectElement element, object target, EventInfo evnt) : base(element, evnt.Name, Helper.GetConverterFor(evnt, evnt.EventHandlerType)) { this.target = target; this.evnt = evnt; }
internal void RegisterKeyItem (XamlObjectElement element, XamlElement target, string key) { IDictionary rd = CurrentDictionary (element); if (rd == null) throw ParseException ("Attempt to use x:Key outside of an IDictionary."); element.X_Key = key; }
internal void RegisterNamedItem (XamlObjectElement element, string name) { IDictionary rd = CurrentDictionary (element); if (rd != null && element.X_Key != null) { throw ParseException ("The name already exists in the tree."); } if (element.X_Name != null) { throw ParseException ("Cannot specify both Name and x:Name attributes."); } element.X_Name = name; FrameworkElement fe = element.FrameworkElement; if (fe != null) fe.SetNameOnScope (name, NameScope); }
public XamlReflectionPropertySetter (XamlObjectElement element, object target, PropertyInfo prop) : base (element, prop.Name, Helper.GetConverterFor (prop, prop.PropertyType)) { this.target = target; this.prop = prop; }
private void ParseObjectElement () { Type t = ResolveType (); if (t == null) throw ParseException ("Unable to find the type {0}.", reader.LocalName); object o = InstantiateType (t); XamlObjectElement element = new XamlObjectElement (this, reader.LocalName, o); SetElementTemplateScopes (element); OnElementBegin (element); ParseElementAttributes (element); // This is a self closing element ie <Rectangle /> if (reader.IsEmptyElement) OnElementEnd (); }
private void AddToDictionary (XamlObjectElement obj, object value) { IDictionary rd = prop.GetValue (target, null) as IDictionary; if (rd == null) throw Parser.ParseException ("Collection property in non collection type."); rd.Add (obj.X_Key, value); }
private void ParseTemplateElement () { Type t = ResolveType (); if (t == null) throw ParseException ("Unable to find the type {0}", t); object o = InstantiateType (t); XamlObjectElement element = new XamlObjectElement (this, reader.LocalName, o); OnElementBegin (element); ParseElementAttributes (element); string template_xml = reader.ReadInnerXml (); FrameworkTemplate template = o as FrameworkTemplate; unsafe { template.SetXamlBuffer (ParseTemplate, CreateXamlContext (template), template_xml); } // // ReadInnerXml will read our closing </ControlTemplate> tag also, so we manually close things // OnElementEnd (); }
public override void SetValue (XamlObjectElement obj, object value) { setter.Invoke (null, new object [] { Element.Object, value }); }
private void ParseTextBlockText (XamlObjectElement block) { }
public static XamlReflectionPropertySetter Create (XamlObjectElement element, object target, Accessors accessors) { if (accessors == null) return null; return new XamlReflectionPropertySetter (element, target, accessors); }
private void ParseElementAttributes (XamlObjectElement element) { if (!reader.HasAttributes) return; try { int ac = reader.AttributeCount; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute (i); ParseAttribute (element); } } finally { // We do this in a finally so error reporting doesn't get all jacked up reader.MoveToElement(); } }
public override void SetValue (XamlObjectElement obj, object value) { MutableObject mutable = value as MutableObject; if (mutable != null) value = mutable.Object; Binding binding = value as Binding; if (binding != null && SetBinding (binding, Target)) return; if (!typeof (TemplateBindingExpression).IsAssignableFrom (Type)) { TemplateBindingExpression tb = value as TemplateBindingExpression; if (tb != null) { SetTemplateBinding (tb, Target); return; } } // We do this before lists to cover the case where you are setting a list to a list or // a resource dictionary to a resource dictionary, ect // as opposed to adding items to the list or dictionary. // // null is a legal value here because they may have done something like foo="{x:Null}" // if (value == null || Type.IsAssignableFrom (value.GetType ())) { accessors.Setter (Target, ConvertValue (Type, value)); return; } if (typeof (IList).IsAssignableFrom (Type)) { AddToCollection (obj, value); return; } if (typeof (IDictionary).IsAssignableFrom (Type)) { AddToDictionary (obj, value); return; } throw Parser.ParseException ("Unable to set property {0} to value {1}.", Name, value); }
private void ParseAttribute (XamlObjectElement element) { if (IsMcAttribute ()) { ParseMcAttribute (element); return; } if (IsXmlnsMapping ()) { ParseXmlnsMapping (element); return; } if (IsXAttribute ()) { ParseXAttribute (element); return; } if (IsXmlDirective ()) { ParseXmlDirective (element); return; } if (IsIgnorable ()) { return; } XamlPropertySetter prop = element.LookupProperty (reader); if (prop == null) throw ParseException ("The property {0} was not found.", reader.LocalName); object value = ParseAttributeValue (element, prop); prop.SetValue (value); }
private void AddToDictionary (XamlObjectElement obj, object value) { IDictionary rd = accessors.Getter (target) as IDictionary; if (rd == null) throw Parser.ParseException ("Collection property in non collection type."); string key = obj.GetDictionaryKey (); if (key == null) throw Parser.ParseException ("You must specify an x:Key or x:Name for elements in a ResourceDictionary"); rd.Add (key, value); }
private void ParseXAttribute (XamlObjectElement element) { switch (reader.LocalName) { case "Key": RegisterKeyItem (element, element.Parent, reader.Value); return; case "Name": RegisterNamedItem (element, reader.Value); return; case "Class": // The class attribute is handled when we initialize the element return; default: throw ParseException ("Unknown x: attribute ({0}).", reader.LocalName); } }
public override void SetValue (XamlObjectElement obj, object value) { MethodInfo invoker_info = evnt.EventHandlerType.GetMethod ("Invoke"); ParameterInfo [] event_params = invoker_info.GetParameters (); string handler_name = value as string; var subscriber = Parser.TopElement; if (subscriber == null) throw Parser.ParseException ("Attempt to set an event handler on an invalid object."); if (String.IsNullOrEmpty (handler_name)) throw Parser.ParseException ("Attmept to set an event handler to null."); Delegate d = null; MethodInfo [] methods = subscriber.GetType ().GetMethods (XamlParser.EVENT_BINDING_FLAGS); MethodInfo candidate = null; bool name_match = false; for (int i = 0; i < methods.Length; i++) { MethodInfo m = methods [i]; ParameterInfo [] parameters; if (m.Name != handler_name) continue; if (name_match) throw Parser.ParseException ("Multiple event handlers found with same name."); name_match = true; parameters = m.GetParameters (); if (parameters.Length != event_params.Length) continue; bool match = true; for (int p = 0; p < parameters.Length; p++) { if (!event_params [p].ParameterType.IsSubclassOf (parameters [p].ParameterType) && parameters [p].ParameterType != event_params [p].ParameterType) { Console.Error.WriteLine ("mismatch: {0} and {1}", parameters [p].ParameterType, event_params [p].ParameterType); match = false; break; } } if (!match) continue; if (candidate != null) throw Parser.ParseException ("Multiple event handler candidates found for event {0}", Name); candidate = m; } if (candidate == null) throw Parser.ParseException ("Event handler not found for event {0}.", Name); d = Delegate.CreateDelegate (evnt.EventHandlerType, subscriber, candidate, false); if (d == null) throw Parser.ParseException ("Unable to create event delegate for event {0}.", Name); evnt.AddEventHandler (target, d); }
private object ParseAttributeValue (XamlObjectElement element, XamlPropertySetter property) { object value = null; if (IsMarkupExpression (reader.Value)) value = ParseAttributeMarkup (element, property); else { value = XamlTypeConverter.ConvertObject (this, element, property.Type, property.Converter, property.Name, reader.Value); } return value; }
public override void SetValue (XamlObjectElement obj, object value) { if (value == null) throw Parser.ParseException ("Setting Name value to null."); string name = value as string; if (name == null) throw Parser.ParseException ("Unable to set Name property to type {1}.", value.GetType ()); obj.X_Name = name; if (!target.SetNameOnScope (name, Parser.NameScope)) throw Parser.ParseException ("Unable to set Name '{0}' on element '{1}'.", name, Element.Name); }
private object ParseAttributeMarkup (XamlObjectElement element, XamlPropertySetter property) { MarkupExpressionParser parser = new SL4MarkupExpressionParser (element.Object, property.Name, this, element); string expression = reader.Value; object o = parser.ParseExpression (ref expression); return o; }
XamlAttachedPropertySetter (XamlObjectElement element, Accessors accessors) : base (element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator ()) { Accessors = accessors; }
private void SetElementTemplateScopes (XamlObjectElement element) { // This whole thing is basically copied from xaml.cpp AddCreatedItem DependencyObject el_dob = element.Object as DependencyObject; if (el_dob == null) return; // When instantiating a template, some elements are created which are not explicitly // mentioned in the xaml. Therefore we need to keep walking up the tree until we find // the last element which we set a value for Control::IsTemplateItem and propagate // it from there. XamlElement instance = CurrentElement; while (instance != null) { XamlObjectElement oe = element as XamlObjectElement; if (oe == null) { instance = instance.Parent; continue; } DependencyObject dob = oe.Object as DependencyObject; if (dob == null) { instance = instance.Parent; continue; } if (dob.ReadLocalValue (Control.IsTemplateItemProperty) == null) { instance = instance.Parent; continue; } el_dob.SetValue (Control.IsTemplateItemProperty, dob.GetValue (Control.IsTemplateItemProperty)); el_dob.TemplateOwner = dob.TemplateOwner; break; } if (instance == null) { el_dob.SetValue (Control.IsTemplateItemProperty, Context.IsExpandingTemplate); el_dob.TemplateOwner = Context.TemplateBindingSource; } if (el_dob.GetValue (Control.IsTemplateItemProperty) != null) { UserControl uc = el_dob as UserControl; if (uc != null) { // I can't come up with a test to verify this fix. However, it does // fix a crasher in olympics when trying to play a new video from // the recommendations list after the curreont video finishes NameScope ns = NameScope.GetNameScope (uc); NameScope.SetNameScope (uc.Content, ns); NameScope.SetNameScope (uc.Resources, ns); } NameScope.SetNameScope (el_dob, NameScope); } }
protected XamlPropertySetter (XamlObjectElement element, string name, TypeConverter converter) { Element = element; Name = name; Converter = converter; }
public abstract void SetValue(XamlObjectElement obj, object value);