次の方法で共有


ColorToHexRgbStringConverter

ColorToHexRgbStringConverter は、ユーザーが Color 値バインディングを #redgreenblue 形式の等価な RGB 16 進数の string に変換できるようにするコンバーターです。ここで redgreenblue は 0 から FF までの値になります (例: Colors.Red の場合は #FF0000)。

Convert メソッドは、指定された Colorvalue を相当する RGB 16 進数 string に変換して返します。

ConvertBack メソッドは、RGB 16 進数 stringvalueColor に変換して返します。

BaseConverter のプロパティ

基底クラス public abstract class BaseConverter には、以下のプロパティが実装されています。

プロパティ 説明
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

ICommunityToolkitValueConverter プロパティ

public interface ICommunityToolkitValueConverter には以下のプロパティが実装されています。

プロパティ タイプ 説明
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue object? IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exception をスローしたときに返される既定値。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

構文

次の例では、ColorToHexRgbStringConverter を使って、特定の Colorの相当する RGB 16 進数文字列を表示する方法を示します。

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>

ColorToHexRgbStringConverter の使用

ColorToHexRgbStringConverter は、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.ColorToHexRgbStringConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:ColorToHexRgbStringConverter x:Key="ColorToHexRgbStringConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <VerticalStackLayout>
        <Label Text="My favourite Color is:" />

        <Label Text="{Binding MyFavoriteColor, Converter={StaticResource ColorToHexRgbStringConverter}}" />
    </VerticalStackLayout>

</ContentPage>

C#

ColorToHexRgbStringConverter は、C# では次のように使用できます。

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

	label.SetBinding(
		Label.TextProperty,
		new Binding(
			nameof(ViewModel.MyFavoriteColor),
			converter: new ColorToHexRgbStringConverter()));

	Content = new VerticalStackLayout
	{
		Children =
		{
			new Label { Text = "My favourite Color is:" },
			label
		}
	};
    }
}

C# Markup

この CommunityToolkit.Maui.Markup パッケージは、C# でこのコンバーターを使用するためのより簡潔な方法を提供します。

using CommunityToolkit.Maui.Markup;

class ColorToHexRgbStringConverterPage : ContentPage
{
    public ColorToHexRgbStringConverterPage()
    {
        Content = new VerticalStackLayout
        {
            Children =
            {
                new Label()
                    .Text("My favourite Color is:"),
                new Label()
                    .Bind(
                        Label.TextProperty,
                        static (ViewModel vm) => vm.MyFavoriteColor,
                        converter: new ColorToHexRgbStringConverter())
            }
        };
    }
}

このコンバーターの動作の例は「.NET MAUI Community Toolkit サンプル アプリケーション」で確認できます。

API

ColorToHexRgbStringConverter のソース コードは、.NET MAUI Community Toolkit の GitHub リポジトリにあります。