IsListNotNullOrEmptyConverter
IsListNotNullOrEmptyConverter
es un convertidor unidireccional que convierte IEnumerable
en un valor bool
.
El método Convert
devuelve false
cuando null
o se pasa un IEnumerable
vacío o true
de lo contrario.
El método ConvertBack
no se admite. Para ver el comportamiento opuesto, consulte IsListNullOrEmptyConverter
.
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 Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
DefaultConvertBackReturnValue |
Valor predeterminado que se devolverá cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produzca Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
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 Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
DefaultConvertBackReturnValue |
object? |
Valor predeterminado que se devolverá cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produzca Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true . |
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>
El IsListNotNullOrEmptyConverter
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="CommunityToolkit.Maui.Sample.Pages.Converters.IsListNotNullOrEmptyConverterPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:IsListNotNullOrEmptyConverter x:Key="IsListNotNullOrEmptyConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Label Text="The list is not empty"
IsVisible="{Binding MyList, Converter={StaticResource IsListNotNullOrEmptyConverter}}" />
</ContentPage>
El IsListNotNullOrEmptyConverter
se puede usar de la siguiente manera en C#:
class IsListNotNullOrEmptyConverterPage : ContentPage
{
public IsListNotNullOrEmptyConverterPage()
{
var label = new Label { Text = "The list is not empty" };
label.SetBinding(
Label.IsVisibleProperty,
new Binding(static (ViewModels vm) => vm.MyList,
converter: new IsListNotNullOrEmptyConverter()));
Content = label;
}
}
Nuestro paquete de CommunityToolkit.Maui.Markup
proporciona una forma mucho más concisa de usar este convertidor en C#.
using CommunityToolkit.Maui.Markup;
class IsListNotNullOrEmptyConverterPage : ContentPage
{
public IsListNotNullOrEmptyConverterPage()
{
Content = new Label { Text = "The list is not empty" }
.Bind(
Label.IsVisibleProperty,
static (ViewModel vm) => vm.MyList,
converter: new IsListNotNullOrEmptyConverter());
}
}
Encontrará un ejemplo de este convertidor en acción en la Aplicación de ejemplo del kit de herramientas de la comunidad de .NET MAUI.
Puede encontrar el código fuente de IsListNotNullOrEmptyConverter
en el repositorio de GitHub del Kit de herramientas de la comunidad de .NET MAUI.
Comentarios de .NET MAUI Community Toolkit
.NET MAUI Community Toolkit es un proyecto de código abierto. Seleccione un vínculo para proporcionar comentarios: