Share via


EnumToBoolConverter

EnumToBoolConverter 변환기는 제공된 열거형 값 집합과 같은지 여부에 따라 해당 bool 값으로 변환 Enum 할 수 있는 단방향 변환기입니다. 열거형 형식을 나타내는 값 컬렉션을 속성과 같은 IsVisible 부울 컨트롤 속성에 바인딩할 때 유용합니다.

메서드는 Convert 정의된 항목과 같은지 또는 제공된 value 것과 같은 TrueValues 지 여부에 value 따라 제공된 변환 boolCommandParameter값을 반환합니다.

ConvertBack 메서드는 지원되지 않습니다.

참고 항목

비교할 'true' 값은 다음과 같은 방법으로 제공할 수 있습니다.

  1. 변환기에서 TrueValue 속성으로 지정합니다.
  2. ConverterParameter 변환기 바인딩에서와 같이

속성이 TrueValues 옵션보다 우선합니다 ConverterParameter .

BaseConverter 속성

다음 속성은 기본 클래스 public abstract class BaseConverter에서 구현됩니다.

속성 설명
DefaultConvertReturnValue 을 throw할 때 IValueConverter.Convert(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.
DefaultConvertBackReturnValue 을 throw할 때 IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.

ICommunityToolkitValueConverter 속성

다음 속성은 다음에서 구현됩니다.public interface ICommunityToolkitValueConverter

속성 Type 설명
DefaultConvertReturnValue object? 을 throw할 때 IValueConverter.Convert(object?, Type, object?, CultureInfo?) 반환할 기본값입니다 Exception. 이 값은 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters가 로 설정된 true경우에 사용됩니다.
DefaultConvertBackReturnValue object? 을 throw할 때 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 사용

XAML EnumToBoolConverter 에서 다음과 같이 사용할 수 있습니다.

<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)
            }
        };
    }
}

속성

속성 Type 설명
TrueValues IList<Enum> (선택 사항)으로 true 변환되는 열거형 값입니다.

예제

.NET MAUI 커뮤니티 도구 키트 샘플 애플리케이션에서 작동 중인 이 변환기의 예를 찾을 수 있습니다.

API

.NET MAUI 커뮤니티 도구 키트 GitHub 리포지토리에서 오버에 대한 EnumToBoolConverter 소스 코드를 찾을 수 있습니다.