IsStringNullOrEmptyConverter

IsStringNullOrEmptyConverter es un convertidor unidireccional que devuelve un bool valor que indica si el valor de enlace es null o string.Empty.

El Convert método devuelve true cuando el enlace value es null o string.Empty.

No se admite el ConvertBack método . Para ver el comportamiento opuesto, vea .IsStringNotNullOrEmptyConverter

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?) genera una excepción 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?) genera una excepción 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?) genera una excepción 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?) genera una excepción Exception. Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está establecido en true.

Syntax

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, lo 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>

Uso del convertidor IsStringNullOrEmptyConverter

El IsStringNullOrEmptyConverter 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.IsStringNullOrEmptyConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:IsStringNullOrEmptyConverter x:Key="IsStringNullOrEmptyConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <Label Text="A value is required"
           IsVisible="{Binding MyValue, Converter={StaticResource IsStringNullOrEmptyConverter}}" />

</ContentPage>

C#

IsStringNullOrEmptyConverter Se puede usar como se indica a continuación en C#:

class IsStringNullOrEmptyConverterPage : ContentPage
{
    public IsStringNullOrEmptyConverterPage()
    {
        var label = new Label { Text = "A value is required" };

        label.SetBinding(
            Label.IsVisibleProperty,
            new Binding(
                static (ViewModels vm) => vm.MyValue,
                converter: new IsStringNullOrEmptyConverter()));

        Content = label;
    }
}

Marcado de C#

Nuestro CommunityToolkit.Maui.Markup paquete proporciona una manera mucho más concisa de usar este convertidor en C#.

using CommunityToolkit.Maui.Markup;

class IsStringNullOrEmptyConverterPage : ContentPage
{
    public IsStringNullOrEmptyConverterPage()
    {
        Content = new Label { Text = "A value is required" }
            .Bind(
                Label.IsVisibleProperty,
                static (ViewModel vm) => vm.MyValue,
                converter: new IsStringNullOrEmptyConverter());
    }
}

Ejemplos

Puede encontrar un ejemplo de este convertidor en acción en la aplicación de ejemplo .NET MAUI Community Toolkit.

API

Puede encontrar el código fuente de IsStringNullOrEmptyConverter en el repositorio .NET MAUI Community Toolkit GitHub.