共用方式為


EnumToIntConverter

EnumToIntConverter是一個轉換器,可讓您將標準 Enum (擴充 int) 轉換為其基礎基本int類型。 將值集合系結成表示具有默認編號的列舉型別,例如 Picker的控件時,會很有用。

注意

屬性為必要屬性,且在使用 或 OneWayToSourceConverterParameter結時TwoWay,應該設定為要轉換回的列舉類型。 否則 ArgumentNullException 會擲回 。 這是允許驗證 int 是否為列舉中的有效值。

基於當地語系化目的或其他需求,列舉值通常需要轉換成人類可讀取的字串。 在此情況下,當用戶選取值時,產生的結果 SelectedIndex 可以輕鬆地轉換成基礎 enum 值,而不需要在相關聯的 ViewModel 中執行額外的工作。

BaseConverter 屬性

基類 public abstract class BaseConverter中實作下列屬性:

屬性 說明
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實作:

屬性 類型​ 描述
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,會使用此值。

語法

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

EnumToIntConverter可以在 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: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, static (ViewModel vm) => vm .AllStates);
        picker.SetBinding(Picker.SelectedItemProperty, static (ViewModel vm) => vm.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 Community Toolkit 範例應用程式中找到此轉換程式的範例

API

您可以在 .NET MAUI Community Toolkit GitHub 存放庫找到 的EnumToIntConverter原始程式碼。