IndexToArrayItemConverter
IndexToArrayItemConverter
es un convertidor que permite a los usuarios convertir un valor int
que enlaza a un elemento de una matriz. El valor int
que se enlaza a datos representa el indexador que se usa para acceder a la matriz. La matriz se pasa a través de ConverterParameter
.
Propiedades de BaseConverter
Las siguientes propiedades se implementan en la clase base, public abstract class BaseConverter
:
Propiedad | Descripción |
---|---|
DefaultConvertReturnValue |
Valor predeterminado que se devuelve cuando IValueConverter.Convert(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
DefaultConvertBackReturnValue |
Valor predeterminado que se devuelve cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
Propiedades de ICommunityToolkitValueConverter
Las siguientes propiedades se implementan en public interface ICommunityToolkitValueConverter
:
Propiedad | Tipo | Descripción |
---|---|---|
DefaultConvertReturnValue |
object? |
Valor predeterminado que se devuelve cuando IValueConverter.Convert(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
DefaultConvertBackReturnValue |
object? |
Valor predeterminado que se devuelve cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produce una Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
Sintaxis
XAML
Incluir el espacio de nombres XAML
Para usar el kit de herramientas en XAML, es necesario agregar el siguiente xmlns
a la página o vista:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Por lo tanto, el siguiente:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
Se modificaría para incluir el xmlns
de la siguiente manera:
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
Al usar IndexToArrayItemConverter
El IndexToArrayItemConverter
se puede usar de la siguiente manera en XAML:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:IndexToArrayItemConverter x:Key="IndexToArrayItemConverter" />
<x:Array x:Key="MyArray" Type="x:String">
<x:String>Value 1</x:String>
<x:String>Value 2</x:String>
<x:String>Value 3</x:String>
<x:String>Value 4</x:String>
<x:String>Value 5</x:String>
</x:Array>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Label Text="{Binding MyIntegerValue, Converter={StaticResource IndexToArrayItemConverter}, ConverterParameter={StaticResource MyArray}}" />
</StackLayout>
</ContentPage>
C#
El IndexToArrayItemConverter
se puede usar de la siguiente manera en C#:
class IndexToArrayItemConverter : ContentPage
{
public IndexToArrayItemConverter()
{
var array = new string[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };
var label = new Label();
label.SetBinding(
Label.TextProperty,
new Binding(
static (ViewModel vm) => vm.MyIntegerValue,
converter: new IndexToArrayItemConverter(),
converterParameter: array));
Content = label;
}
}
Marcado de C#
Nuestro paquete de CommunityToolkit.Maui.Markup
proporciona una forma mucho más concisa de usar este convertidor en C#.
using CommunityToolkit.Maui.Markup;
class IndexToArrayItemConverter : ContentPage
{
public IndexToArrayItemConverter()
{
var array = new string[] { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };
Content = new Label()
.Bind(
Label.TextProperty,
static (ViewModel vm) => vm.MyIntegerValue,
converter: new IndexToArrayItemConverter(),
converterParameter: array);
}
}
Ejemplos
Puede encontrar un ejemplo de este convertidor en acción en la Aplicación de muestra del kit de herramientas de la comunidad de .NET MAUI.
API
Puede encontrar el código fuente de IndexToArrayItemConverter
en el repositorio de GitHub del Kit de herramientas de la comunidad de .NET MAUI.
.NET MAUI Community Toolkit