IsStringNullOrEmptyConverterは、バインディング値が null かboolかを示すstring.Emptyを返す一方向コンバーターです。
Convert メソッドは、バインディング trueがvalueまたはnullされたときにstring.Emptyを返します。
ConvertBack メソッドはサポートされていません。 逆の動作については、 IsStringNotNullOrEmptyConverterを参照してください。
BaseConverter のプロパティ
基底クラスでは、次のプロパティが実装 public abstract class BaseConverter。
| 財産 | Description |
|---|---|
DefaultConvertReturnValue |
IValueConverter.Convert(object?, Type, object?, CultureInfo?)がExceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)がExceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
ICommunityToolkitValueConverter プロパティ
public interface ICommunityToolkitValueConverterでは、次のプロパティが実装されています。
| 財産 | タイプ | Description |
|---|---|---|
DefaultConvertReturnValue |
object? |
IValueConverter.Convert(object?, Type, object?, CultureInfo?)がExceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
object? |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)がExceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
Syntax
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(
static (ViewModels vm) => vm.MyValue,
converter: new IsStringNullOrEmptyConverter()));
Content = label;
}
}
C# マークアップ
この 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 リポジトリで確認できます。
.NET MAUI Community Toolkit