public static object Convert(IBindingMemberInfo member, Type type, object value) { if (XamarinFormsToolkitExtensions.ValueConverter != null) { return(XamarinFormsToolkitExtensions.ValueConverter(member.Member as MemberInfo, type, value)); } if (value == null) { return(type.GetDefaultValue()); } if (type.IsInstanceOfType(value)) { return(value); } if (BindingExtensions.IsConvertible(value)) { return(System.Convert.ChangeType(value, type.GetNonNullableType(), BindingServiceProvider.BindingCultureInfo())); } if (type.GetTypeInfo().IsEnum) { var s = value as string; if (s != null) { return(Enum.Parse(type, s, false)); } return(Enum.ToObject(type, value)); } if (type == typeof(string)) { return(value.ToString()); } return(value); }
public virtual object InvokeDynamicMethod(string methodName, IDataContext context, IList <Type> typeArgs, object[] items) { var resourceMethod = BindingServiceProvider.ResourceResolver.ResolveMethod(methodName, context, false); if (resourceMethod != null) { return(resourceMethod.Invoke(typeArgs, items, context)); } var binding = context.GetData(BindingConstants.Binding); Type targetType = binding == null ? typeof(object) : binding.TargetAccessor.Source.GetPathMembers(false).LastMember.Type; var converter = BindingServiceProvider.ResourceResolver.ResolveConverter(methodName, context, true); return(converter.Convert(items[0], targetType, items.Length > 1 ? items[1] : null, items.Length > 2 ? (CultureInfo)items[2] : BindingServiceProvider.BindingCultureInfo(), context)); }
internal static object Convert(IBindingMemberInfo member, Type type, object value) { if (value == null) { return(type.GetDefaultValue()); } if (type.IsInstanceOfType(value)) { return(value); } #if NET_STANDARD if (type.GetTypeInfo().IsEnum) #else if (type.IsEnum) #endif { var s = value as string; if (s == null) { return(Enum.ToObject(type, value)); } return(Enum.Parse(type, s, false)); } #if WPF || ANDROID || TOUCH || WINFORMS || WINDOWS_PHONE var converter = GetTypeConverter(type, member.Member); if (converter != null && converter.CanConvertFrom(value.GetType())) { return(converter.ConvertFrom(value)); } #endif #if NET_STANDARD if (TypeCodeTable.ContainsKey(value.GetType())) #else if (value is IConvertible) #endif { return(System.Convert.ChangeType(value, type.GetNonNullableType(), BindingServiceProvider.BindingCultureInfo())); } if (type == typeof(string)) { return(value.ToString()); } return(value); }
internal static object Convert(IBindingMemberInfo member, Type type, object value) { if (value == null) { if (type.IsValueType() && !type.IsNullableType()) { return(Activator.CreateInstance(type)); } return(null); } if (type.IsInstanceOfType(value)) { return(value); } #if PCL_WINRT if (type.GetTypeInfo().IsEnum&& value is string) #else if (type.IsEnum && value is string) #endif { return(Enum.Parse(type, (string)value, false)); } #if WPF || ANDROID || TOUCH || WINFORMS || WINDOWS_PHONE || SILVERLIGHT var converter = GetTypeConverter(type, member.Member); if (converter != null && converter.CanConvertFrom(value.GetType())) { return(converter.ConvertFrom(value)); } #endif #if PCL_WINRT if (TypeCodeTable.ContainsKey(value.GetType())) #else if (value is IConvertible) #endif { return(System.Convert.ChangeType(value, type.GetNonNullableType(), BindingServiceProvider.BindingCultureInfo())); } if (type == typeof(string)) { return(value.ToString()); } return(value); }
private object GetSourceValue(IBindingMemberInfo targetMember, IDataContext context, bool throwOnError) { object value = GetRawValueInternal(targetMember, context, throwOnError); if (value.IsDoNothing()) { return(BindingConstants.DoNothing); } if (_parameters.ConverterDelegate != null && !value.IsUnsetValue()) { IBindingValueConverter converter = _parameters.ConverterDelegate(context); if (converter != null) { CultureInfo culture = _parameters.ConverterCultureDelegate.GetValueOrDefault(context, BindingServiceProvider.BindingCultureInfo()); object parameter = _parameters.ConverterParameterDelegate.GetValueOrDefault(context); value = converter.Convert(value, targetMember.Type, parameter, culture, context); } } if (value.IsUnsetValue()) { value = _parameters.FallbackDelegate.GetValueOrDefault(context) ?? targetMember.Type.GetDefaultValue(); } if (value == null) { return(_parameters.TargetNullValue); } return(value); }
private object GetTargetValue(IBindingMemberInfo targetMember, IDataContext context, bool throwOnError) { object value = GetRawValueInternal(targetMember, context, throwOnError); if (value.IsUnsetValueOrDoNothing()) { return(value); } if (_parameters.ConverterDelegate != null) { IBindingValueConverter converter = _parameters.ConverterDelegate(context); if (converter != null) { CultureInfo culture = _parameters.ConverterCultureDelegate.GetValueOrDefault(context, BindingServiceProvider.BindingCultureInfo()); object parameter = _parameters.ConverterParameterDelegate.GetValueOrDefault(context); value = converter.ConvertBack(value, targetMember.Type, parameter, culture, context); } } if (Equals(value, _parameters.TargetNullValue)) { return(null); } return(value); }
private object GetSourceValue(IBindingMemberInfo targetMember, IDataContext context, bool throwOnError) { var isDebuggable = IsDebuggable; object value = GetRawValueInternal(targetMember, context, throwOnError); if (isDebuggable) { DebugInfo($"Got a source value: '{value}'"); } if (value.IsDoNothing()) { return(BindingConstants.DoNothing); } if (_parameters.ConverterDelegate != null && !value.IsUnsetValue()) { IBindingValueConverter converter = _parameters.ConverterDelegate(context); if (converter != null) { CultureInfo culture = _parameters.ConverterCultureDelegate?.Invoke(context) ?? BindingServiceProvider.BindingCultureInfo(); object parameter = _parameters.ConverterParameterDelegate?.Invoke(context); if (isDebuggable) { DebugInfo($"Applying converter for source value: '{value}', converter: '{converter}', parameter: '{parameter}', culture: {culture}, target type: '{targetMember.Type}'"); value = converter.Convert(value, targetMember.Type, parameter, culture, context); DebugInfo($"Converter '{converter}' returns value: '{value}'"); } else { value = converter.Convert(value, targetMember.Type, parameter, culture, context); } } } if (value.IsUnsetValue()) { value = _parameters.FallbackDelegate?.Invoke(context) ?? targetMember.Type.GetDefaultValue(); } if (value == null) { return(_parameters.TargetNullValue); } return(value); }
private object GetTargetValue(IBindingMemberInfo targetMember, IDataContext context, bool throwOnError) { var isDebuggable = IsDebuggable; object value = GetRawValueInternal(targetMember, context, throwOnError); if (isDebuggable) { DebugInfo($"Got a target value: '{value}'"); } if (value.IsUnsetValueOrDoNothing()) { return(value); } IBindingValueConverter converter = _parameters.ConverterDelegate?.Invoke(context); if (converter != null) { CultureInfo culture = _parameters.ConverterCultureDelegate?.Invoke(context) ?? BindingServiceProvider.BindingCultureInfo(); object parameter = _parameters.ConverterParameterDelegate?.Invoke(context); if (isDebuggable) { DebugInfo($"Applying converter for target value: '{value}', converter: '{converter}', parameter: '{parameter}', culture: {culture}, target type: '{targetMember.Type}'"); value = converter.ConvertBack(value, targetMember.Type, parameter, culture, context); DebugInfo($"Converter '{converter}' returns value: '{value}'"); } else { value = converter.ConvertBack(value, targetMember.Type, parameter, culture, context); } } if (Equals(value, _parameters.TargetNullValue)) { if (isDebuggable) { DebugInfo("Target value equals to TargetNullValue, return null value"); } return(null); } return(value); }