Merge pull request #57 from wcoder/value_converter_group

Multiple converters
This commit is contained in:
Depechie 2017-11-13 18:42:16 +01:00 коммит произвёл GitHub
Родитель fa6471a683 a7a2143239
Коммит 5b823f32a4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 126 добавлений и 0 удалений

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

@ -0,0 +1,24 @@
# ValueConverterGroup
Multiple converters.
Conversion values using the sequential conversion of each of the converters from the first to the last.
## Syntax
Define in ResourceDictionary:
```xml
<converters:ValueConverterGroup x:Key="IsEmptyInverse">
<converters:IsEmptyConverter />
<converters:InvertedBooleanConverter />
</converters:ValueConverterGroup>
```
## Example
```xml
<Button Text="Save"
Command="{Binding AddItemCommand}"
IsVisible="{Binding ItemName, Converter={StaticResource IsEmptyInverse}}" />
```

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

@ -56,8 +56,10 @@
<Compile Include="HexToColorTests.cs" />
<Compile Include="InvertedBooleanTests.cs" />
<Compile Include="LowerTextTests.cs" />
<Compile Include="Mocks\MockConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpperTextTests.cs" />
<Compile Include="ValueConverterGroupTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Converters\Converters.csproj">

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

@ -0,0 +1,30 @@
using System;
using System.Globalization;
using Xamarin.Forms;
namespace Converters.Tests.Mocks
{
public class MockConverter : IValueConverter
{
public const string TagFormat = "c{0}<-";
public int Id { get; }
public MockConverter()
{
Id = 0;
}
public MockConverter(int id)
{
Id = id;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format(TagFormat, Id) + value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => null;
}
}

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

@ -0,0 +1,37 @@
using System.Globalization;
using NUnit.Framework;
using FormsCommunityToolkit.Converters;
using Converters.Tests.Mocks;
namespace Converters.Tests
{
[TestFixture]
public class ValueConverterGroupTests
{
ValueConverterGroup converter;
[SetUp]
public void Setup()
{
converter = new ValueConverterGroup();
}
[TestCase("Test_Value")]
public void ConvertList(object value)
{
var expectedResult = value.ToString();
for (int i = 0; i < 10; i++)
{
var c = new MockConverter(i);
converter.Add(c);
expectedResult = string.Format(MockConverter.TagFormat, i) + expectedResult;
}
var actualResult = converter.Convert(value, typeof(string), null, CultureInfo.CurrentCulture);
Assert.AreEqual(expectedResult, actualResult);
}
}
}

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

@ -45,6 +45,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpperTextConverter.cs" />
<Compile Include="ValueConversionAttribute.cs" />
<Compile Include="ValueConverterGroup.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Xamarin.Forms.Core, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">

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

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Xamarin.Forms;
namespace FormsCommunityToolkit.Converters
{
/// <summary>
/// Conversion values using the sequential conversion of each of the converters from the first to the last.
/// </summary>
[ValueConversion(typeof(IEnumerator<IValueConverter>), typeof(IEnumerator<IValueConverter>))]
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
{
public static ValueConverterGroup Instance { get; } = new ValueConverterGroup();
/// <summary>
/// Convert the value using a sequence of converters, from first to last.
/// </summary>
/// <param name="value">The source data being passed to the target.</param>
/// <param name="targetType">The type of the target property. Used for all converters.</param>
/// <param name="parameter">An optional parameter to be used in the converter logic all converters.</param>
/// <param name="culture">The language of the conversion. Used for all converters.</param>
/// <returns>The value to be passed to the target dependency property. Result of executing a sequence of converters.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return this.Aggregate(value, (current, converter) => converter.Convert(current, targetType, parameter, culture));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => null;
}
}