Share via


StateToBooleanConverter

제공 StateToBooleanConverter 된 값이 특정 LayoutState값인지 여부에 따라 결과를 반환 boolean 하는 단방향 변환기입니다.

이 메서드는 Convert 제공된 값이 특정 LayoutState값인지 여부에 따라 결과를 반환 boolean 합니다. LayoutState 열거형은 도구 키트에서 제공하며 가능한 값을 제공합니다.

  • None
  • Loading
  • Saving
  • Success
  • Error
  • Empty
  • Custom

참고 항목

다음 우선 순위에 따라 예상 LayoutState 값을 제공할 수 있습니다.

  1. ConverterParameter 변환기 바인딩에서와 같이 속성을 대체합니다StateToCompare.
  2. 변환기에서 StateToCompare 속성으로

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

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경우에 사용됩니다.

구문

다음 예제에서는 변환기를 사용 하 여에서 수정 ButtonCommand되는 속성에 LayoutState 따라 컨트롤의 Label 표시 유형을 변경 하는 방법을 보여 집니다.

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>

StateToBooleanConverter 사용

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

<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.StateToBooleanConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:StateToBooleanConverter x:Key="StateToBooleanConverter" StateToCompare="Success" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout VerticalOptions="Center">
        <Label
            HorizontalOptions="Center"
            IsVisible="{Binding LayoutState, Converter={StaticResource StateToBooleanConverter}}"
            Text="The state is Success!"
            VerticalOptions="Center" />
        <Button Command="{Binding ChangeLayoutCommand}" Text="Change state" />
    </VerticalStackLayout>

</ContentPage>

C#

StateToBooleanConverter C#에서 다음과 같이 사용할 수 있습니다.

class StateToBooleanConverterPage : ContentPage
{
    public StateToBooleanConverterPage()
    {
        var label = new Label
        {
            HorizontalOptions = LayoutOptions.Center,
            Text = "The state is Success!",
            VerticalOptions = LayoutOptions.Center
        };

        label.SetBinding(
            Label.IsVisibleProperty,
            new Binding(
                nameof(ViewModel.LayoutState),
                converter: new StateToBooleanConverter { StateToCompare = LayoutState.Success }));

        var button = new Button
        {
            Text = "Change state"
        };
    
        button.SetBinding(
            Button.CommandProperty,
            nameof(ViewModel.ChangeLayoutCommand));

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

C# 태그

CommunityToolkit.Maui.Markup 패키지는 C#에서 이 변환기를 사용하는 훨씬 더 간결한 방법을 제공합니다.

using CommunityToolkit.Maui.Markup;

class StateToBooleanConverterPage : ContentPage
{
    public StateToBooleanConverterPage()
    {
        Content = new VerticalStackLayout
        {
            Children = 
            {
                new Label()
                    .Text("The state is Success!")
                    .CenterHorizontal()
                    .CenterVertical()
                    .Bind(
                        Label.IsVisibleProperty,
                        static (ViewModel vm) => vm.LayoutState,
                        converter: new StateToBooleanConverter { StateToCompare = LayoutState.Success }),

                new Button()
                    .Text("Change state")
                    .BindCommand(static (ViewModel vm) => vm.ChangeLayoutCommand)
            }
        };
    }
}

예제

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

API

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