EnumToBoolConverter

EnumToBoolConverter 是一种单向转换器,允许根据 Enum 是否等于一组提供的枚举值将其转换为相应的 bool。 将表示枚举类型的值集合绑定到布尔控件属性(如 IsVisible 属性)时,它很有用。

Convert 方法返回提供的 value,并基于 value 是否等于定义的 TrueValues 或提供的 CommandParameter 中的任何一个将其转换为 bool

不支持 ConvertBack 方法。

注意

请注意,可通过以下方式提供要进行比较的“true”值:

  1. 作为转换器上的 TrueValue 属性。
  2. 作为转换器绑定中的 ConverterParameter

请注意,TrueValues 属性将优先于 ConverterParameter 选项。

BaseConverter 属性

以下属性是在基类 public abstract class BaseConverter 中实现的:

properties 说明
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 中实现:

properties 类型​​ 描述
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 时,将使用此值。

语法

以下每个示例都使用以下枚举定义:

namespace MyLittleApp;

public enum MyDevicePlatform
{
    Android,
    iOS,
    macOS,
    Tizen,
    Windows
}

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>

使用 EnumToBoolConverter

EnumToBoolConverter 可以在 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"
             xmlns:mylittleapp="clr-namespace:MyLittleApp"
             x:Class="MyLittleApp.MainPage">
    <ContentPage.Resources>
         <ResourceDictionary>
             <toolkit:EnumToBoolConverter x:Key="MobileConverter">
                <toolkit:EnumToBoolConverter.TrueValues>
                    <mylittleapp:MyDevicePlatform>Android</mylittleapp:MyDevicePlatform>
                    <mylittleapp:MyDevicePlatform>iOS</mylittleapp:MyDevicePlatform>
                </toolkit:EnumToBoolConverter.TrueValues>
            </toolkit:EnumToBoolConverter>
         </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout>
        <Picker ItemsSource="{Binding Platforms}"
                SelectedItem="{Binding SelectedPlatform}" />
        <Label IsVisible="{Binding SelectedPlatform, Converter={StaticResource MobileConverter}}"
               Text="I am visible when the Picker value is Android or iOS."/>
    </VerticalStackLayout>
</ContentPage>

还可以传递转换器参数:

<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="MyLittleApp.MainPage">
    <ContentPage.Resources>
         <ResourceDictionary>
             <toolkit:EnumToBoolConverter x:Key="PlatformConverter" />
         </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout>
        <Picker ItemsSource="{Binding Platforms}"
                SelectedItem="{Binding SelectedPlatform}" />
        <Label IsVisible="{Binding SelectedPlatform, Converter={StaticResource PlatformConverter}, ConverterParameter={x:Static vm:MyDevicePlatform.Tizen}}"
               Text="I am visible when the Picker value is Tizen."/>
    </VerticalStackLayout>
</ContentPage>

C#

EnumToBoolConverter 可在 C# 中按如下所示方式使用:

class EnumToBoolConverterPage : ContentPage
{
    public EnumToBoolConverterPage()
    {
        var picker = new Picker();
        picker.SetBinding(Picker.ItemsSourceProperty, nameof(ViewModel.Platforms));
        picker.SetBinding(Picker.SelectedItemProperty, nameof(ViewModel.SelectedPlatform));

        var label = new Label
        {
            Text = "I am visible when the Picker value is Tizen."
        };

		label.SetBinding(
			Label.IsVisibleProperty,
			new Binding(
				nameof(ViewModel.SelectedPlatform),
				converter: new EnumToBoolConverter(),
                converterParameter: MyDevicePlatform.Tizen));

		Content = new VerticalStackLayout
        {
            Children = { picker, label }
        };
    }
}

C# 标记

我们的 CommunityToolkit.Maui.Markup 包提供了在 C# 中使用此转换器的更简洁的方法。

using CommunityToolkit.Maui.Markup;

class EnumToBoolConverterPage : ContentPage
{
    public EnumToBoolConverterPage()
    {
        Content = new VerticalStackLayout
        {
            Children = 
            {
                new Picker()
                    .Bind(
                        Picker.ItemsSourceProperty, 
                        static (ViewModel vm) => vm.Platforms)
                    .Bind(
                        Picker.SelectedItemProperty,
                        static (ViewModel vm) => vm.SelectedPlatform),

                new Label()
                    .Text("I am visible when the Picker value is Tizen.")
                    .Bind(
                        Label.IsVisibleProperty,
                        static (ViewModel vm) => vm.SelectedPlatform,
                        converter: new EnumToBoolConverter(),
                        converterParameter: MyDevicePlatform.Tizen)
            }
        };
    }
}

属性

属性 类型​​ 描述
TrueValues IList<Enum> 转换为 true 的枚举值(可选)。

示例

可以在 .NET MAUI 社区工具包示例应用程序中找到此转换器的示例。

API

可以在 .NET MAUI 社区工具包 GitHub 存储库查看EnumToBoolConverter 的源代码