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 static DependencyProperty ConvertDependencyProperty(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string)ovalue; Type target_type; int idx = value.IndexOf('.'); if (idx > 0) { target_type = converter.parser.ResolveType(value.Substring(0, idx)); value = value.Substring(idx + 1, value.Length - idx - 1); } else { target_type = GetTargetType(converter); } Types.Ensure(target_type); ManagedType mt = Deployment.Current.Types.Find(target_type); DependencyProperty dp = XamlParser.LookupDependencyProperty((Kind)mt.native_handle, value); return(dp); }
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); }
private static object ConvertPropertyPath(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { string typename; string propertyname; string index; PropertyNodeType node; string str = (string)value; // Fastpath - if there are no prefixed types then we have nothing to expand if (!str.Contains(":")) { return(new PropertyPath(str)); } var parser = new PropertyPathParser(str); var expanded = new StringBuilder(); while ((node = parser.Step(out typename, out propertyname, out index)) != PropertyNodeType.None) { switch (node) { case PropertyNodeType.AttachedProperty: if (expanded.Length > 0) { expanded.Append('.'); } if (typename.Contains(":")) { typename = converter.parser.ResolveType(typename).ToString(); expanded.AppendFormat("('{0}'.{1})", typename, propertyname); } else { expanded.AppendFormat("({0}.{1})", typename, propertyname); } break; case PropertyNodeType.Indexed: expanded.AppendFormat("[{0}]", index); break; case PropertyNodeType.Property: if (expanded.Length > 0) { expanded.Append('.'); } expanded.Append(propertyname); break; default: throw new Exception(string.Format("Could not handle PropertyNodeType.{0}", node)); } } return(new PropertyPath(str, expanded.ToString())); }
public object ConvertValue(Type type, object value) { if (value == null) { return(null); } if (value is Binding || value is TemplateBindingExpression) { return(value); } MutableObject mutable = value as MutableObject; if (mutable != null) { value = mutable.Object; } Type valueType = value.GetType(); if (type.IsAssignableFrom(valueType)) { return(value); } TypeConverter converter = Converter; if (converter == null) { try { converter = new XamlTypeConverter(Parser, Element, Name, type); } catch (Exception e) { Console.Error.WriteLine("Exception while creating type converter (this is a recoverable error.)"); Console.Error.WriteLine(e); converter = null; } } if (converter != null && converter.CanConvertFrom(valueType)) { return(converter.ConvertFrom(value)); } try { if (!valueType.IsSubclassOf(type)) { value = Convert.ChangeType(value, type, System.Globalization.CultureInfo.CurrentCulture); } } catch { } // This will just let things fail return(value); }
private static object ConvertDouble(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { string str = (string)value; if (StringComparer.OrdinalIgnoreCase.Equals(str, "Auto")) { return(Double.NaN); } return(null); }
private static Type ConvertType(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string)ovalue; Type t = converter.parser.ResolveType(value); if (t == null) { Console.Error.WriteLine("could not convert type from: '{0}'", value); } return(t); }
protected override PropertyPath ParsePropertyPath(string piece) { var converter = new XamlTypeConverter(parser, target_element, AttributeName, typeof(PropertyPath)); var path = converter.ConvertFrom(piece) as PropertyPath; if (path == null) { Console.Error.WriteLine("Error parsing property path: '{0}'.", piece); return(null); } return(path); }
private static RoutedEvent ConvertRoutedEventArgs(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string)ovalue; int dot = value.IndexOf("."); if (dot < 1) { throw new XamlParseException("Invalid format for RoutedEvent."); } string type_name = value.Substring(0, dot); string event_name = value.Substring(dot + 1, value.Length - dot - 1); Type type = converter.parser.LoadType(null, converter.parser.Current.DefaultXmlns, type_name); RoutedEvent res = null; Type eventids = typeof(EventIds); // Try UIElement first since thats where most of these events live if (typeof(UIElement).IsAssignableFrom(type)) { res = RoutedEvent(eventids, "UIElement", event_name); if (res != null) { return(res); } } if (!type.IsValueType) { Type walk = type; while (walk != typeof(object)) { res = RoutedEvent(eventids, walk.Name, event_name); if (res != null) { return(res); } walk = walk.BaseType; } } return(null); }
private static object ConvertPropertyPath (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { string typename; string propertyname; string index; PropertyNodeType node; string str = (string) value; // Fastpath - if there are no prefixed types then we have nothing to expand if (!str.Contains (":")) return new PropertyPath (str); var parser = new PropertyPathParser (str); var expanded = new StringBuilder (); while ((node = parser.Step (out typename, out propertyname, out index)) != PropertyNodeType.None) { switch (node) { case PropertyNodeType.AttachedProperty: if (expanded.Length > 0) expanded.Append ('.'); if (typename.Contains (":")) { typename = converter.parser.ResolveType (typename).ToString (); expanded.AppendFormat ("('{0}'.{1})", typename, propertyname); } else { expanded.AppendFormat ("({0}.{1})", typename, propertyname); } break; case PropertyNodeType.Indexed: expanded.AppendFormat ("[{0}]", index); break; case PropertyNodeType.Property: if (expanded.Length > 0) expanded.Append ('.'); expanded.Append (propertyname); break; default: throw new Exception (string.Format ("Could not handle PropertyNodeType.{0}", node)); } } return new PropertyPath (str, expanded.ToString ()); }
protected override PropertyPath ParsePropertyPath (string piece) { var converter = new XamlTypeConverter (parser, target_element, AttributeName, typeof (PropertyPath)); var path = converter.ConvertFrom (piece) as PropertyPath; if (path == null) { Console.Error.WriteLine ("Error parsing property path: '{0}'.", piece); return null; } return path; }
private static object ConvertDouble (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { string str = (string) value; if (str == "Auto") return Double.NaN; return null; }
private static object ConvertPropertyPath (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { string str = (string) value; StringBuilder expanded = new StringBuilder (str); bool has_expanded = false; for (int i = 0; i < expanded.Length; i++) { if (expanded [i] == ':') { int e = i; int s = i - 1; int te = i + 1; for ( ; s > 0; s--) { if (!Char.IsLetterOrDigit (expanded [s])) break; } for ( ; te < str.Length; te++) { if (!Char.IsLetterOrDigit (expanded [te]) || expanded [te] == '_') break; } string prefix = expanded.ToString (s + 1, e - s - 1); string type = expanded.ToString (e + 1, te - e - 1); expanded.Remove (s + 1, te - s - 1); string ns = null; if (!converter.parser.Context.Xmlns.TryGetValue (prefix, out ns)) { Console.WriteLine ("could not find xmlns value: {0}", prefix); return null; } Type t = converter.parser.ResolveType (prefix + ":" + type); string uri = String.Format ("'{0}'", t); expanded.Insert (s + 1, uri); i = s + 1 + uri.Length; has_expanded = true; } } if (has_expanded) return new PropertyPath (str, expanded.ToString ()); return new PropertyPath (str); }
private static object ConvertXmlLanguage (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { return XmlLanguage.GetLanguage ((string) value); }
public object ConvertValue (Type type, object value) { if (value == null) return null; Type valueType = value.GetType (); if (valueType == type) return value; TypeConverter converter = Converter; if (converter == null) { try { converter = new XamlTypeConverter (Parser, Element, Name, type); } catch { converter = null; } } if (converter != null && converter.CanConvertFrom (valueType)) return converter.ConvertFrom (value); try { if (!valueType.IsSubclassOf (type)) value = Convert.ChangeType (value, type, System.Globalization.CultureInfo.CurrentCulture); } catch { } // This will just let things fail return value; }
public virtual object ConvertTextValue(string value) { return(XamlTypeConverter.ConvertObject(Parser, Element, Type, Converter, Name, value)); }
private static object ConvertColor(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { object res = Color.FromString((string)value); return(res); }
private static RoutedEvent ConvertRoutedEventArgs (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string) ovalue; int dot = value.IndexOf ("."); if (dot < 1) throw new XamlParseException ("Invalid format for RoutedEvent."); string type_name = value.Substring (0, dot); string event_name = value.Substring (dot + 1, value.Length - dot - 1); Type type = converter.parser.LoadType (null, converter.parser.Context.DefaultXmlns, type_name); RoutedEvent res = null; Type eventids = typeof (EventIds); // Try UIElement first since thats where most of these events live if (typeof (UIElement).IsAssignableFrom (type)) { res = RoutedEvent (eventids, "UIElement", event_name); if (res != null) { Console.WriteLine ("returning routed event: {0}", res); return res; } } if (!type.IsValueType) { Type walk = type; while (walk != typeof (object)) { res = RoutedEvent (eventids, walk.Name, event_name); if (res != null) return res; walk = walk.BaseType; } } return null; }
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 static object ConvertDouble (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { string str = (string) value; if (StringComparer.OrdinalIgnoreCase.Equals (str, "Auto")) return Double.NaN; return null; }
private static DependencyProperty ConvertDependencyProperty (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string) ovalue; Type target_type = GetTargetType (converter); Types.Ensure (target_type); ManagedType mt = Deployment.Current.Types.Find (target_type); DependencyProperty dp = DependencyProperty.Lookup ((Kind) mt.native_handle, value); return dp; }
private static Type ConvertType (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string) ovalue; Type t = converter.parser.ResolveType (value); if (t == null) Console.Error.WriteLine ("could not convert type from: '{0}'", value); return t; }
private static Type ConvertType (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string) ovalue; Console.WriteLine ("attempting to load type: {0}", value); return converter.parser.ResolveType (value); }
private static object ConvertXmlLanguage(XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { return(XmlLanguage.GetLanguage((string)value)); }
private static DependencyProperty ConvertDependencyProperty (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object ovalue) { string value = (string) ovalue; Type target_type; int idx = value.IndexOf ('.'); if (idx > 0) { target_type = converter.parser.ResolveType (value.Substring (0, idx)); value = value.Substring (idx + 1, value.Length - idx - 1); } else target_type = GetTargetType (converter); Types.Ensure (target_type); ManagedType mt = Deployment.Current.Types.Find (target_type); DependencyProperty dp = XamlParser.LookupDependencyProperty ((Kind) mt.native_handle, value); return dp; }
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; }
private static object ConvertColor (XamlTypeConverter converter, ITypeDescriptorContext context, CultureInfo culture, object value) { object res = Color.FromString ((string) value); return res; }
public object ConvertValue (Type type, object value) { if (value == null) return null; if (value is Binding || value is TemplateBindingExpression) return value; MutableObject mutable = value as MutableObject; if (mutable != null) value = mutable.Object; Type valueType = value.GetType (); if (type.IsAssignableFrom (valueType)) return value; TypeConverter converter = Converter; if (converter == null) { try { converter = new XamlTypeConverter (Parser, Element, Name, type); } catch (Exception e) { Console.Error.WriteLine ("Exception while creating type converter (this is a recoverable error.)"); Console.Error.WriteLine (e); converter = null; } } if (converter != null && converter.CanConvertFrom (valueType)) return converter.ConvertFrom (value); try { if (!valueType.IsSubclassOf (type)) value = Convert.ChangeType (value, type, System.Globalization.CultureInfo.CurrentCulture); } catch { } // This will just let things fail return value; }
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) converter = new XamlTypeConverter (parser, element, prop_name, dest_type); if (!converter.CanConvertFrom (val.GetType ())) throw new Exception (string.Format ("type converter {0} can't convert from type {1} destination type: {2}", converter.GetType (), val.GetType (), dest_type)); return converter.ConvertFrom (null, Helper.DefaultCulture, val); }