diff options
author | Rui Marinho <me@ruimarinho.net> | 2016-03-29 15:13:26 +0100 |
---|---|---|
committer | Rui Marinho <me@ruimarinho.net> | 2016-03-29 18:53:12 +0100 |
commit | 99b27e9658425da98c6216cc1f804c42d3f43d6f (patch) | |
tree | e0e6f7c2290a03864fead4436914e1dcddbdf175 /Xamarin.Forms.Core | |
parent | 57b0f3ab3080928d2ddd2da58fc84ef7023c3651 (diff) | |
download | xamarin-forms-99b27e9658425da98c6216cc1f804c42d3f43d6f.tar.gz xamarin-forms-99b27e9658425da98c6216cc1f804c42d3f43d6f.tar.bz2 xamarin-forms-99b27e9658425da98c6216cc1f804c42d3f43d6f.zip |
[Controls,Core]Allow well known type conversions on the binding system
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r-- | Xamarin.Forms.Core/BindableProperty.cs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/BindableProperty.cs b/Xamarin.Forms.Core/BindableProperty.cs index f95d38ab..5eb330ff 100644 --- a/Xamarin.Forms.Core/BindableProperty.cs +++ b/Xamarin.Forms.Core/BindableProperty.cs @@ -30,6 +30,12 @@ namespace Xamarin.Forms public delegate bool ValidateValueDelegate<in TPropertyType>(BindableObject bindable, TPropertyType value); + static readonly Dictionary<Type, TypeConverter> WellKnownConvertTypes = new Dictionary<Type,TypeConverter> + { + { typeof(Uri), new UriTypeConverter() }, + { typeof(Color), new ColorTypeConverter() }, + }; + // more or less the encoding of this, without the need to reflect // http://msdn.microsoft.com/en-us/library/y5b434w4.aspx static readonly Dictionary<Type, Type[]> SimpleConvertTypes = new Dictionary<Type, Type[]> @@ -303,10 +309,15 @@ namespace Xamarin.Forms // Dont support arbitrary IConvertible by limiting which types can use this Type[] convertableTo; + TypeConverter typeConverterTo; if (SimpleConvertTypes.TryGetValue(valueType, out convertableTo) && Array.IndexOf(convertableTo, type) != -1) { value = Convert.ChangeType(value, type); } + else if (WellKnownConvertTypes.TryGetValue(type, out typeConverterTo) && typeConverterTo.CanConvertFrom(valueType)) + { + value = typeConverterTo.ConvertFromInvariantString(value.ToString()); + } else if (!ReturnTypeInfo.IsAssignableFrom(valueType.GetTypeInfo())) { // Is there an implicit cast operator ? |