Last active
November 25, 2020 14:25
-
-
Save KirillAshikhmin/76e742fe6abeebd94c6bae4ebb1debeb to your computer and use it in GitHub Desktop.
Xamarin.Forms inline ValueConverter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections; | |
| using System.Diagnostics; | |
| using System.Globalization; | |
| using Xamarin.Forms; | |
| namespace Project.UI.Converters | |
| { | |
| public static class ConverterFactory | |
| { | |
| public static IValueConverter Make([NotNull] Func<object, object> func, object defaultValue = null, bool allowDefault = false, Func<object, object> convertBack = null) => | |
| new Converter(func, defaultValue) { AllowDefault = allowDefault, ConvertBackFunc = convertBack }; | |
| public static IValueConverter Make<T>([NotNull] Func<T, object> func, object defaultValue = null, bool allowDefault = false, Func<object, object> convertBack = null) => | |
| new Converter<T>(func, defaultValue) { AllowDefault = allowDefault, ConvertBackFunc = convertBack }; | |
| public static IValueConverter MakeString([NotNull] Func<string, object> func, object defaultValue = null, bool allowDefault = false, Func<object, string> convertBack = null) => | |
| new Converter<string>(func, defaultValue) { IsString = true, AllowDefault = allowDefault, ConvertBackFunc = convertBack }; | |
| public static IValueConverter MakeBool([NotNull] Func<bool, object> func, object defaultValue = null, bool allowDefault = false, Func<object, object> convertBack = null) => | |
| new Converter<bool>(func, defaultValue) { IsString = false, AllowDefault = allowDefault, ConvertBackFunc = convertBack }; | |
| public static IValueConverter FromDictionary([NotNull] IDictionary dictionary, object defaultValue = null) => | |
| new DictionaryConverter(dictionary, defaultValue); | |
| class Converter : IValueConverter | |
| { | |
| readonly object _defaultValue; | |
| readonly Func<object, object> _func; | |
| public bool IsString { get; set; } | |
| public bool AllowDefault { get; set; } | |
| public Func<object, object> ConvertBackFunc { get; set; } | |
| public Converter(Func<object, object> func, object defaultValue = null) | |
| { | |
| _func = func; | |
| _defaultValue = defaultValue; | |
| } | |
| public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
| { | |
| try | |
| { | |
| if (!AllowDefault && (IsString && string.IsNullOrEmpty((string)value) || !IsString && value == null)) return _defaultValue; | |
| return _func(value); | |
| } | |
| catch (Exception e) | |
| { | |
| Debug.WriteLine(e); | |
| return _defaultValue; | |
| } | |
| } | |
| public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
| { | |
| if (ConvertBackFunc == null) throw new NotImplementedException(); | |
| return ConvertBackFunc?.Invoke(value); | |
| } | |
| } | |
| class Converter<T> : Converter | |
| { | |
| public Converter(Func<T, object> func, object defaultValue = null) : base(o => func.Invoke((T)o), defaultValue) | |
| { | |
| } | |
| } | |
| class DictionaryConverter : IValueConverter | |
| { | |
| readonly IDictionary _dictionary; | |
| readonly object _defaultValue; | |
| public DictionaryConverter(IDictionary dictionary, object defaultValue = null) | |
| { | |
| _dictionary = dictionary; | |
| _defaultValue = defaultValue; | |
| } | |
| public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => | |
| value != null && _dictionary.Contains(value) ? _dictionary[value] : _defaultValue; | |
| public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment