Skip to content

Guoldev/added enumtolistconverter #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions samples/XCT.Sample/Pages/Converters/EnumToListConverterPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8" ?>
<pages:BasePage
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Converters.EnumToListConverterPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Converters"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
<pages:BasePage.Resources>
<ResourceDictionary>
<xct:EnumToListConverter x:Key="EnumToListConverter" />
<xct:ObjectToNameConverter x:Key="ObjectToNameConverter" />
</ResourceDictionary>
</pages:BasePage.Resources>
<pages:BasePage.BindingContext>
<vm:EnumToListConverterViewModel />
</pages:BasePage.BindingContext>
<pages:BasePage.Content>
<Grid Margin="20" RowDefinitions="Auto,Auto,Auto,Auto,Auto,*">
<Label
Grid.Row="0"
FontAttributes="Bold"
Text="Select Enum Type:" />
<Picker
Grid.Row="1"
BackgroundColor="AliceBlue"
ItemDisplayBinding="{Binding ., Converter={StaticResource ObjectToNameConverter}}"
ItemsSource="{Binding EnumTypes}"
SelectedItem="{Binding SelectedEnumType}" />
<Label
Grid.Row="2"
Margin="0,20,0,0"
FontAttributes="Bold"
Text="Select Enum Value from Picker:" />
<Picker
Grid.Row="3"
ItemsSource="{Binding SelectedEnumType, Converter={StaticResource EnumToListConverter}}"
SelectedItem="{Binding SelectedItem}" />
<Label
Grid.Row="4"
Margin="0,20,0,0"
FontAttributes="Bold"
Text="Select Enum Value from ListView:" />
<ListView
Grid.Row="5"
HasUnevenRows="True"
ItemsSource="{Binding SelectedEnumType, Converter={StaticResource EnumToListConverter}}"
SelectedItem="{Binding SelectedItem}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label
Margin="0,20"
FontSize="Medium"
Text="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</pages:BasePage.Content>
</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class EnumToListConverterPage : BasePage
{
public EnumToListConverterPage()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ protected override IEnumerable<SectionModel> CreateItems() => new[]
typeof(StringToListConverterPage),
nameof(StringToListConverter),
"A converter that splits a string by the separator and returns the enumerable sequence of strings as the result."),
new SectionModel(
typeof(EnumToListConverterPage),
nameof(EnumToListConverter),
"A converter that returns the enumerable sequence of enum as the result."),
new SectionModel(
typeof(CompareConverterPage),
nameof(CompareConverter),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.CommunityToolkit.Behaviors;
using Xamarin.CommunityToolkit.UI.Views;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Converters
{
public enum CustomEnum
{
A,
B,
C,
D,
E,
F
}

public class EnumToListConverterViewModel : BaseViewModel
{
List<Type> enumTypes;
Type? selectedEnumType;
object? selectedItem = null;

public EnumToListConverterViewModel()
{
enumTypes = new List<Type>() { typeof(CustomEnum), typeof(BadgePosition), typeof(CameraCaptureMode), typeof(CharacterType) };
selectedEnumType = enumTypes.FirstOrDefault();
}

public List<Type> EnumTypes
{
get => enumTypes;
set => SetProperty(ref enumTypes, value);
}

public Type? SelectedEnumType
{
get => selectedEnumType;
set => SetProperty(ref selectedEnumType, value);
}

public object? SelectedItem
{
get => selectedItem;
set => SetProperty(ref selectedItem, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public void StringToListConverter(object value, object parameter, object expecte
[TestCase(0)]
public void InValidConverterValuesThrowArgumenException(object value)
{
var listToStringConverter = new ListToStringConverter();
var stringToListConverter = new StringToListConverter();

Assert.Throws<ArgumentException>(() => listToStringConverter.Convert(value, null, null, null));
Assert.Throws<ArgumentException>(() => stringToListConverter.Convert(value, null, null, null));
}

[TestCase(0)]
public void InValidConverterParametersThrowArgumenException(object parameter)
{
var listToStringConverter = new ListToStringConverter();
var stringToListConverter = new StringToListConverter();

Assert.Throws<ArgumentException>(() => listToStringConverter.Convert(new object[0], null, parameter, null));
Assert.Throws<ArgumentException>(() => stringToListConverter.Convert(new object[0], null, parameter, null));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Globalization;
using Xamarin.CommunityToolkit.Extensions.Internals;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Converters
{
/// <summary>
/// A converter that retrieves an array of the values of the constants in a specified enumeration.
/// </summary>
public class EnumToListConverter : ValueConverterExtension, IValueConverter
{
/// <summary>
/// Retrieves an array of the values of the constants in a specified enumeration.
/// </summary>
/// <param name="value">The Enum.</param>
/// <param name="targetType">The type of the binding target property. This is not implemented.</param>
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>An array that contains the values of the constants in <paramref name="value"/>.</returns>
public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
{
if (value is Type type)
return Enum.GetValues(type);
return value;
}

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="value">N/A</param>
/// <param name="targetType">N/A</param>
/// <param name="parameter">N/A</param>
/// <param name="culture">N/A</param>
/// <returns>N/A</returns>
public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture)
=> throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Globalization;
using Xamarin.CommunityToolkit.Extensions.Internals;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Converters
{
/// <summary>
/// A converter that returns the name of the object.
/// </summary>
public class ObjectToNameConverter : ValueConverterExtension, IValueConverter
{
/// <summary>
/// Returns the name of the name of the object.
/// </summary>
/// <param name="value">The string to split.</param>
/// <param name="targetType">The type of the binding target property. This is not implemented.</param>
/// <param name="parameter">The string or strings that delimits the substrings in this string. This overrides the value in <see cref="Separator"/> and <see cref="Separators"/>.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>An array whose elements contain the substrings in this string that are delimited by <see cref="Separator"/> or, if set, <see cref="Separators"/> or, if set, <paramref name="parameter"/>.</returns>
public object? Convert(object value, Type? targetType, object? parameter, CultureInfo? culture)
{
if (value is Type type)
return type.Name;

return string.Empty;
}

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="value">N/A</param>
/// <param name="targetType">N/A</param>
/// <param name="parameter">N/A</param>
/// <param name="culture">N/A</param>
/// <returns>N/A</returns>
public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture)
=> throw new NotImplementedException();
}
}