Bagikan melalui


TextCaseConverter

TextCaseConverter adalah pengonversi satu arah yang memungkinkan pengguna untuk mengonversi casing pengikatan string jenis masuk. Properti Type digunakan untuk menentukan jenis casing apa yang akan diterapkan ke string.

Metode mengembalikan Convert yang disediakan value yang dikonversi ke yang ditentukan TextCaseType. Perhatikan bahwa TextCaseType dapat disediakan dengan cara berikut:

  1. sebagai dalam ConverterParameter pengikatan konverter,
  2. Type sebagai properti pada pengonversi.

Perhatikan bahwa ConverterParameter opsi akan lebih diutamakan daripada Type properti .

Metode ConvertBack ini tidak didukung.

Properti BaseConverter

Properti berikut diimplementasikan di kelas dasar, public abstract class BaseConverter:

Properti Deskripsi
DefaultConvertReturnValue Nilai default untuk dikembalikan saat IValueConverter.Convert(object?, Type, object?, CultureInfo?) melempar Exception. Nilai ini digunakan ketika CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters diatur ke true.
DefaultConvertBackReturnValue Nilai default untuk dikembalikan saat IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) melempar Exception. Nilai ini digunakan ketika CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters diatur ke true.

Properti ICommunityToolkitValueConverter

Properti berikut diimplementasikan dalam public interface ICommunityToolkitValueConverter:

Properti Tipe Deskripsi
DefaultConvertReturnValue object? Nilai default untuk dikembalikan saat IValueConverter.Convert(object?, Type, object?, CultureInfo?) melempar Exception. Nilai ini digunakan ketika CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters diatur ke true.
DefaultConvertBackReturnValue object? Nilai default untuk dikembalikan saat IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) melempar Exception. Nilai ini digunakan ketika CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters diatur ke true.

Sintaks

XAML

Menyertakan namespace XAML

Untuk menggunakan toolkit di XAML, hal berikut xmlns perlu ditambahkan ke halaman atau tampilan Anda:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Oleh karena itu hal-hal berikut:

<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>

Akan dimodifikasi untuk menyertakan xmlns sebagai berikut:

<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>

Menggunakan TextCaseConverter

TextCaseConverter dapat digunakan sebagai berikut dalam 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"
             x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.TextCaseConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:TextCaseConverter x:Key="TextCaseConverter" Type="Upper" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <Label Text="{Binding MyValue, Converter={StaticResource TextCaseConverter}}" />

</ContentPage>

C#

TextCaseConverter dapat digunakan sebagai berikut di C#:


class TextCaseConverterPage : ContentPage
{
    public TextCaseConverterPage()
    {
        var label = new Label();

        label.SetBinding(
            Label.TextProperty,
            new Binding(
                static (ViewModels vm) => vm.MyValue,
                converter: new TextCaseConverter { Type = TextCaseType.Upper }));

        Content = label;
    }
}

C# Markup

Paket kami CommunityToolkit.Maui.Markup menyediakan cara yang jauh lebih ringkas untuk menggunakan pengonversi ini di C#.

using CommunityToolkit.Maui.Markup;

class TextCaseConverterPage : ContentPage
{
    public TextCaseConverterPage()
    {
        Content = new Label()
            .Bind(
                Label.TextProperty,
                static (ViewModel vm) => vm.MyValue,
                converter: new TextCaseConverter { Type = TextCaseType.Upper });
    }
}

Properti

Properti Tipe Deskripsi
Jenis TextCaseType Jenis casing yang akan diterapkan ke string nilai .

TextCaseType

Enumerasi TextCaseType menentukan anggota berikut:

  • None - Tidak menerapkan pemformatan khusus pada string.
  • Upper - Menerapkan pemformatan huruf besar ke string.
  • Lower - Menerapkan pemformatan huruf kecil ke string.
  • FirstUpperRestLower - Menerapkan pemformatan huruf besar ke karakter pertama lalu pemformatan huruf kecil ke string yang tersisa.

Contoh

Anda dapat menemukan contoh pengonversi ini dalam tindakan di Aplikasi Sampel Toolkit Komunitas .NET MAUI.

API

Anda dapat menemukan kode sumber untuk TextCaseConverter lebih pada repositori GitHub .NET MAUI Community Toolkit.