From 99b27e9658425da98c6216cc1f804c42d3f43d6f Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Tue, 29 Mar 2016 15:13:26 +0100 Subject: [Controls,Core]Allow well known type conversions on the binding system --- Xamarin.Forms.Core/BindableProperty.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'Xamarin.Forms.Core') 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(BindableObject bindable, TPropertyType value); + static readonly Dictionary WellKnownConvertTypes = new Dictionary + { + { 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 SimpleConvertTypes = new Dictionary @@ -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 ? -- cgit v1.2.3