Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
O IsStringNullOrEmptyConverter é um conversor unidirecional que devolve um bool indicando se o valor de ligação é nulo ou string.Empty.
O Convert método retorna true quando a ligação value é null ou string.Empty.
O ConvertBack método não é suportado. Para o comportamento oposto, veja o IsStringNotNullOrEmptyConverter.
Propriedades do BaseConverter
As seguintes propriedades são implementadas na classe base, public abstract class BaseConverter:
| Propriedade | Description |
|---|---|
DefaultConvertReturnValue |
Valor padrão a devolver quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) lança um Exception. Este valor é usado quando o CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está definido como true. |
DefaultConvertBackReturnValue |
Valor padrão a devolver quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) lança um Exception. Este valor é usado quando o CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está definido como true. |
Propriedades do ICommunityToolkitValueConverter
As seguintes propriedades estão implementadas no public interface ICommunityToolkitValueConverter:
| Propriedade | Tipo | Description |
|---|---|---|
DefaultConvertReturnValue |
object? |
Valor padrão a devolver quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) lança um Exception. Este valor é usado quando o CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está definido como true. |
DefaultConvertBackReturnValue |
object? |
Valor padrão a devolver quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) lança um Exception. Este valor é usado quando o CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters está definido como true. |
Syntax
XAML
Incluindo o namespace XAML
Para usar o kit de ferramentas em XAML, a seguinte xmlns precisa ser adicionada à sua página ou vista.
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Por conseguinte, 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 da seguinte forma:
<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>
Usando o IsStringNullOrEmptyConverter
O IsStringNullOrEmptyConverter pode ser utilizado da seguinte forma 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#
O IsStringNullOrEmptyConverter pode ser usado da seguinte forma 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#
O nosso CommunityToolkit.Maui.Markup pacote oferece uma forma muito mais concisa de usar este 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
Pode encontrar um exemplo deste conversor em ação na .NET MAUI Community Toolkit Sample Application.
API
Você pode encontrar o código-fonte para IsStringNullOrEmptyConverter no repositório GitHub do .NET MAUI Community Toolkit.
.NET MAUI Community Toolkit