Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
O IsStringNullOrEmptyConverter é um conversor unidirecional que retorna um bool indicando se o valor da associação é nulo ou string.Empty.
O Convert método retorna true quando a associação value é null ou string.Empty.
O método ConvertBack não é suportado. Para o comportamento oposto, consulte o IsStringNotNullOrEmptyConverter.
Propriedades do BaseConverter
As seguintes propriedades são implementadas na classe base: public abstract class BaseConverter
| Property | Descrição |
|---|---|
DefaultConvertReturnValue |
Valor padrão a ser retornado quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true. |
DefaultConvertBackReturnValue |
Valor padrão a ser retornado quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true. |
Propriedades de ICommunityToolkitValueConverter
As seguintes propriedades são implementadas no public interface ICommunityToolkitValueConverter:
| Property | Tipo | Descrição |
|---|---|---|
DefaultConvertReturnValue |
object? |
Valor padrão a ser retornado quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true. |
DefaultConvertBackReturnValue |
object? |
Valor padrão a ser retornado quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true. |
Syntax
XAML
Incluir o namespace XAML
Para usar o kit de ferramentas no XAML, o xmlns a seguir precisa ser adicionado à sua página ou exibição:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Portanto, o seguinte:
<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>
Seria modificado para incluir o xmlns conforme o seguinte:
<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>
Como usar o IsStringNullOrEmptyConverter
Pode IsStringNullOrEmptyConverter ser usado da seguinte maneira em 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#
Pode IsStringNullOrEmptyConverter ser usado da seguinte maneira em 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;
}
}
Marcação em C#
Nosso CommunityToolkit.Maui.Markup pacote fornece uma maneira muito mais concisa de usar esse conversor em 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());
}
}
Exemplos
Você pode encontrar um exemplo desse conversor em ação no aplicativo de exemplo .NET MAUI Community Toolkit.
API
O código-fonte do IsStringNullOrEmptyConverter pode ser encontrado no repositório GitHub do .NET MAUI Community Toolkit.
.NET MAUI Community Toolkit