EnumToIntConverter
표준 EnumToIntConverter
(int 확장)을 기본 기본 형식으로 변환 Enum
할 수 있는 변환기입니다 int
. 열거형 형식을 나타내는 값 컬렉션을 기본 번호 매기기를 사용하여 컨트롤 Picker
에 바인딩할 때 유용합니다.
참고 항목
ConverterParameter
속성은 필수이며, 다시 변환할 열거형의 형식으로 설정해야 합니다( 또는 OneWayToSource
바인딩을 사용할 TwoWay
때). 그렇지 않으면 throw ArgumentNullException
됩니다. 이는 열거형의 유효한 값인지 여부를 int
확인할 수 있도록 하기 위한 것입니다.
지역화를 위해 또는 다른 요구 사항으로 인해 열거형 값을 사람이 읽을 수 있는 문자열로 변환해야 하는 경우가 많습니다. 이 경우 사용자가 값을 선택하면 연결된 ViewModel에서 추가 작업을 요구하지 않고도 결과를 SelectedIndex
기본 enum
값으로 쉽게 변환할 수 있습니다.
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 경우에 사용됩니다. |
구문
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>
EnumToIntConverter 사용
XAML EnumToIntConverter
에서 다음과 같이 사용할 수 있습니다.
<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:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Converters"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:EnumToIntConverter x:Key="EnumToIntConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout Padding="10,10" Spacing="10">
<Label Text="The EnumToIntConverter is a converter that allows users to convert a standard enum (extending int) to its underlying primitive int type."
TextColor="{StaticResource NormalLabelTextColor}" />
<Label Text="Selecting a value from the picker will change the enum property in the view model"
TextColor="{StaticResource NormalLabelTextColor}" />
<Picker ItemsSource="{Binding AllStates}"
SelectedIndex="{Binding SelectedState, Converter={StaticResource EnumToIntConverter}, ConverterParameter={x:Type vm:IssueState}}"
TextColor="{StaticResource NormalLabelTextColor}" />
<Label Text="This label binds to the SelectedIndex property of the picker, both use EnumToIntConverter, so no int properties are necessary in ViewModel"
TextColor="{StaticResource NormalLabelTextColor}" />
<Label Text="{Binding Path=SelectedState, Converter={StaticResource EnumToIntConverter}}"
TextColor="{StaticResource NormalLabelTextColor}" />
</VerticalStackLayout>
</ContentPage>
C#
EnumToIntConverter
C#에서 다음과 같이 사용할 수 있습니다.
class EnumToIntConverterPage : ContentPage
{
public EnumToIntConverterPage()
{
Picker picker = new Picker { Title = "EnumToIntConverter" };
picker.SetBinding(Picker.ItemsSourceProperty, nameof(ViewModel.AllStates));
picker.SetBinding(Picker.SelectedItemProperty, nameof(ViewModel.SelectedState));
Content = new StackLayout
{
Margin = new Thickness(20),
Children = {
new Label {
Text = "The EnumToIntConverter is a converter that allows users to convert a standard enum (extending int) to its underlying primitive int type.",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
},
picker
}
};
}
C# 태그
이 CommunityToolkit.Maui.Markup
패키지는 C#에서 이 변환기를 사용하는 훨씬 더 간결한 방법을 제공합니다.
using CommunityToolkit.Maui.Markup;
class EnumToIntConverterPage : ContentPage
{
public EnumToIntConverterPage()
{
Content = new StackLayout {
new Picker()
.Bind(
Picker.ItemSourceProperty,
static (ViewModel vm) => vm.AllStates)
.Bind(
Picker.SelectedIndexProperty,
static (ViewModel vm) => vm.SelectedState),
new Label()
.Bind(
Label.TextProperty,
static (ViewModel vm) => vm.SelectedState,
converter: new EnumToIntConverter()),
}
}
}
예제
.NET MAUI 커뮤니티 도구 키트 샘플 애플리케이션에서 작동 중인 이 변환기의 예를 찾을 수 있습니다.
API
.NET MAUI 커뮤니티 도구 키트 GitHub 리포지토리에서 오버에 대한 EnumToIntConverter
소스 코드를 찾을 수 있습니다.
.NET MAUI Community Toolkit
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기