ColorToHslaStringConverter
ColorToHslaStringConverter
es un convertidor unidireccional que permite a los usuarios enlazar un valor de Color
a su equivalente de string
HSLA en el formato: HSLA(tono,saturación,luminosidad) donde tono será un valor entre 0 y 360, y saturación y luminosidad serán un valor entre 0 % y 100 %, y alfa será un valor entre 0 y 1 (por ejemplo, HSLA(0,100 %,50 %,1) para Colors.Red
.
El método Convert
devuelve el Color
value
proporcionado convertido a su equivalente de string
HSLA.
No se admite el método ConvertBack
.
Propiedades de BaseConverter
Las siguientes propiedades se implementan en la clase base, public abstract class BaseConverter
:
Propiedad | Descripción |
---|---|
DefaultConvertReturnValue |
Valor predeterminado que se devuelve cuando IValueConverter.Convert(object?, Type, object?, CultureInfo?) produce Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters se establece en true . |
DefaultConvertBackReturnValue |
Valor predeterminado que se devuelve cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produce Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters se establece en true . |
Propiedades de ICommunityToolkitValueConverter
Las siguientes propiedades se implementan en public interface ICommunityToolkitValueConverter
:
Propiedad | Tipo | Descripción |
---|---|---|
DefaultConvertReturnValue |
object? |
Valor predeterminado que se devuelve cuando IValueConverter.Convert(object?, Type, object?, CultureInfo?) produce Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters se establece en true . |
DefaultConvertBackReturnValue |
object? |
Valor predeterminado que se devuelve cuando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) produce Exception . Este valor se usa cuando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters se establece en true . |
Sintaxis
En los ejemplos siguientes se muestra cómo usar ColorToHslaStringConverter
para mostrar la cadena equivalente de HSLA de un determinado Color
.
XAML
Incluir el espacio de nombres XAML
Para usar el kit de herramientas en XAML, es necesario agregar el siguiente xmlns
a la página o vista:
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Por lo tanto, el siguiente:
<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>
Se modificaría para incluir el xmlns
de la siguiente manera:
<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 de ColorToHslaStringConverter
El ColorToHslaStringConverter
se puede usar de la siguiente manera en 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.ColorToHslaStringConverterPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:ColorToHslaStringConverter x:Key="ColorToHslaStringConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout>
<Label Text="My favourite Color is:" />
<Label Text="{Binding MyFavoriteColor, Converter={StaticResource ColorToHslaStringConverter}}" />
</VerticalStackLayout>
</ContentPage>
C#
El ColorToHslaStringConverter
se puede usar de la siguiente manera en C#:
class ColorToHslaStringConverterPage : ContentPage
{
public ColorToHslaStringConverterPage()
{
var label = new Label();
label.SetBinding(
Label.TextProperty,
new Binding(
static (ViewModel vm) => vm.MyFavoriteColor,
converter: new ColorToHslaStringConverter()));
Content = new VerticalStackLayout
{
Children =
{
new Label { Text = "My favourite Color is:" },
label
}
};
}
}
Marcado de C#
Nuestro paquete CommunityToolkit.Maui.Markup
proporciona una forma mucho más concisa de usar este convertidor en C#.
using CommunityToolkit.Maui.Markup;
class ColorToHslaStringConverterPage : ContentPage
{
public ColorToHslaStringConverterPage()
{
Content = new VerticalStackLayout
{
Children =
{
new Label()
.Text("My favourite Color is:"),
new Label()
.Bind(
Label.TextProperty,
static (ViewModel vm) => vm.MyFavoriteColor,
converter: new ColorToHslaStringConverter())
}
};
}
}
Ejemplos
Encontrará un ejemplo de este convertidor en acción en la Aplicación de ejemplo del kit de herramientas de la comunidad de .NET MAUI.
API
Puede encontrar el código fuente de ColorToHslaStringConverter
en el repositorio de GitHub del Kit de herramientas de la comunidad de .NET MAUI.
.NET MAUI Community Toolkit