Share via


IsStringNullOrEmptyConverter

IsStringNullOrEmptyConverter は、バインディング値が null または string.Empty であるかどうかを示す bool を返す一方向のコンバーターです。

Convert メソッドは、バインディング valuenull または string.Empty の場合に true を返します。

ConvertBack メソッドはサポートされていません。 逆の動作については、IsStringNotNullOrEmptyConverter を参照してください。

BaseConverter プロパティ

基底クラス public abstract class BaseConverter には、次のプロパティが実装されています。

プロパティ 説明
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

ICommunityToolkitValueConverter プロパティ

public interface ICommunityToolkitValueConverter には次のプロパティが実装されています。

プロパティ タイプ 説明
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue object? IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

構文

XAML

XAML 名前空間を含める

XAML でこのツールキットを使用するには、次の xmlns をページまたはビューに追加する必要があります。

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

したがって、以下のコードは、

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

次のように、xmlns を含むように変更されます。

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

IsStringNullOrEmptyConverter の使用

IsStringNullOrEmptyConverter は、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 は、C# では次のように使用できます。

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

		label.SetBinding(
			Label.IsVisibleProperty,
			new Binding(
				nameof(ViewModels.MyValue),
				converter: new IsStringNullOrEmptyConverter()));

		Content = label;
    }
}

C# Markup

この CommunityToolkit.Maui.Markup パッケージは、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());
    }
}

このコンバーターの動作の例は「.NET MAUI Community Toolkit サンプル アプリケーション」で確認できます。

API

IsStringNullOrEmptyConverter のソース コードは、.NET MAUI Community Toolkit の GitHub リポジトリにあります。