And share the TryConvert implementation to avoid future diverges
This commit is contained in:
Stephane Delcroix 2018-11-20 02:11:51 +01:00 коммит произвёл Samantha Houts
Родитель 4871781069
Коммит 4f789b9bda
2 изменённых файлов: 8 добавлений и 37 удалений

Просмотреть файл

@ -164,7 +164,7 @@ namespace Xamarin.Forms
else
value = Binding.FallbackValue ?? property.DefaultValue;
if (!TryConvert(part, ref value, property.ReturnType, true))
if (!TryConvert(ref value, property, property.ReturnType, true))
{
Log.Warning("Binding", "{0} can not be converted to type '{1}'", value, property.ReturnType);
return;
@ -176,7 +176,7 @@ namespace Xamarin.Forms
{
object value = Binding.GetTargetValue(target.GetValue(property), part.SetterType);
if (!TryConvert(part, ref value, part.SetterType, false))
if (!TryConvert(ref value, property, part.SetterType, false))
{
Log.Warning("Binding", "{0} can not be converted to type '{1}'", value, part.SetterType);
return;
@ -415,16 +415,15 @@ namespace Xamarin.Forms
}
static Type[] DecimalTypes = new[] { typeof(float), typeof(decimal), typeof(double) };
bool TryConvert(BindingExpressionPart part, ref object value, Type convertTo, bool toTarget)
internal static bool TryConvert(ref object value, BindableProperty targetProperty, Type convertTo, bool toTarget)
{
if (value == null)
return true;
if ((toTarget && _targetProperty.TryConvert(ref value)) || (!toTarget && convertTo.IsInstanceOfType(value)))
if ((toTarget && targetProperty.TryConvert(ref value)) || (!toTarget && convertTo.IsInstanceOfType(value)))
return true;
object original = value;
try
{
try {
var stringValue = value as string ?? string.Empty;
// see: https://bugzilla.xamarin.com/show_bug.cgi?id=32871
// do not canonicalize "*.[.]"; "1." should not update bound BindableProperty
@ -442,18 +441,7 @@ namespace Xamarin.Forms
value = Convert.ChangeType(value, convertTo, CultureInfo.InvariantCulture);
return true;
}
catch (InvalidCastException)
{
value = original;
return false;
}
catch (FormatException)
{
value = original;
return false;
}
catch (OverflowException)
{
catch (Exception ex) when (ex is InvalidCastException || ex is FormatException || ex is OverflowException) {
value = original;
return false;
}

Просмотреть файл

@ -203,7 +203,7 @@ namespace Xamarin.Forms.Internals
} catch (Exception ex) when (ex is NullReferenceException || ex is KeyNotFoundException) {
}
}
if (!TryConvert(ref value, property, property.ReturnType, true)) {
if (!BindingExpression.TryConvert(ref value, property, property.ReturnType, true)) {
Log.Warning("Binding", "{0} can not be converted to type '{1}'", value, property.ReturnType);
return;
}
@ -214,7 +214,7 @@ namespace Xamarin.Forms.Internals
var needsSetter = (mode == BindingMode.TwoWay && fromTarget) || mode == BindingMode.OneWayToSource;
if (needsSetter && _setter != null && isTSource) {
var value = GetTargetValue(target.GetValue(property), typeof(TProperty));
if (!TryConvert(ref value, property, typeof(TProperty), false)) {
if (!BindingExpression.TryConvert(ref value, property, typeof(TProperty), false)) {
Log.Warning("Binding", "{0} can not be converted to type '{1}'", value, typeof(TProperty));
return;
}
@ -222,23 +222,6 @@ namespace Xamarin.Forms.Internals
}
}
static bool TryConvert(ref object value, BindableProperty targetProperty, Type convertTo, bool toTarget)
{
if (value == null)
return true;
if ((toTarget && targetProperty.TryConvert(ref value)) || (!toTarget && convertTo.IsInstanceOfType(value)))
return true;
object original = value;
try {
value = Convert.ChangeType(value, convertTo, CultureInfo.InvariantCulture);
return true;
} catch (Exception ex ) when (ex is InvalidCastException || ex is FormatException||ex is OverflowException) {
value = original;
return false;
}
}
class PropertyChangedProxy
{
public Func<TSource, object> PartGetter { get; }