Condividi tramite


ColorToDegreeHueConverter

ColorToDegreeHueConverter è un convertitore unidirezionale che consente agli utenti di convertire un oggetto in ingresso Color nel componente hue come valore compreso tra 0 e 360. La tonalità è un grado sulla ruota dei colori da 0 a 360. 0 è rosso, 120 è verde, 240 è blu.

Il Convert metodo restituisce il componente hue come valore compreso tra 0 e 360 dall'oggetto fornito value.

Il ConvertBack metodo non è supportato.

Proprietà BaseConverter

Le proprietà seguenti vengono implementate nella classe base : public abstract class BaseConverter

Proprietà Descrizione
DefaultConvertReturnValue Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.
DefaultConvertBackReturnValue Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.

Proprietà ICommunityToolkitValueConverter

Le proprietà seguenti vengono implementate in public interface ICommunityToolkitValueConverter:

Proprietà Type Descrizione
DefaultConvertReturnValue object? Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.
DefaultConvertBackReturnValue object? Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un'eccezione Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.

Sintassi

Gli esempi seguenti illustrano come usare per visualizzare il componente hue di un oggetto specificoColor.ColorToDegreeHueConverter

XAML

Inclusione dello spazio dei nomi XAML

Per usare il toolkit in XAML, è necessario aggiungere le informazioni seguenti xmlns nella pagina o nella visualizzazione:

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

Di conseguenza:

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

Verrà modificato in modo da includere l'oggetto xmlns come indicato di seguito:

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

Uso di ColorToDegreeHueConverter

Può ColorToDegreeHueConverter essere usato come segue in 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.ColorToDegreeHueConverterPage">

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

    <VerticalStackLayout>
         <Label Text="The hue component is:" />

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

</ContentPage>

C#

Può ColorToDegreeHueConverter essere usato come indicato di seguito in C#:

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

        label.SetBinding(
            Label.TextProperty,
            new Binding(
                static (ViewModel vm) => vm.MyFavoriteColor,
                converter: new ColorToDegreeHueConverter()));

        Content = new VerticalStackLayout
        {
            Children =
            {
                new Label { Text = "The hue component is:" },
                label
            }
        };
    }
}

C# Markup

Il CommunityToolkit.Maui.Markup pacchetto offre un modo molto più conciso per usare questo convertitore in C#.

using CommunityToolkit.Maui.Markup;

class ColorToDegreeHueConverterPage : ContentPage
{
    public ColorToDegreeHueConverterPage()
    {
        Content = new VerticalStackLayout
        {
            Children =
            {
                new Label()
                    .Text("The hue component is:"),
                new Label()
                    .Bind(
                        Label.TextProperty,
                        static (ViewModel vm) => vm.MyFavoriteColor,
                        converter: new ColorToDegreeHueConverter())
            }
        };
    }
}

Esempi

È possibile trovare un esempio di questo convertitore in azione nell'applicazione di esempio .NET MAUI Community Toolkit.

API

È possibile trovare il codice sorgente per ColorToDegreeHueConverter over nel repository GitHub di .NET MAUI Community Toolkit.