title | author | description | ms.date |
---|---|---|---|
ColorToRgbaStringConverter - .NET MAUI Community Toolkit |
bijington |
The ColorToRgbaStringConverter is a converter that allows users to convert a Color value binding to its RGBA string equivalent. |
02/15/2024 |
The ColorToRgbaStringConverter
is a converter that allows users to convert a Color
value binding to its RGBA string
equivalent in the format: RGB(red,green,blue,alpha) where red, green and blue will be a value between 0 and 255, and alpha is a value between 0 and 1 (e.g. RGB(255,0,0,1) for Colors.Red
.
The Convert
method returns the supplied Color
value
converted to its RGB string
equivalent.
The ConvertBack
method returns the RGB string
value
converted to a Color
.
[!INCLUDE common converter properties]
The following examples will show how to use the ColorToRgbaStringConverter
to display the RGBA equivalent string of a specific Color
.
[!INCLUDE XAML usage guidance]
The ColorToRgbaStringConverter
can be used as follows 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.ColorToRgbaStringConverterPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:ColorToRgbaStringConverter x:Key="ColorToRgbaStringConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout>
<Label Text="My favourite Color is:" />
<Label Text="{Binding MyFavoriteColor, Converter={StaticResource ColorToRgbaStringConverter}}" />
</VerticalStackLayout>
</ContentPage>
The ColorToRgbaStringConverter
can be used as follows in C#:
class ColorToRgbaStringConverterPage : ContentPage
{
public ColorToRgbaStringConverterPage()
{
var label = new Label();
label.SetBinding(
Label.TextProperty,
new Binding(
nameof(ViewModel.MyFavoriteColor),
converter: new ColorToRgbaStringConverter()));
Content = new VerticalStackLayout
{
Children =
{
new Label { Text = "My favourite Color is:" },
label
}
};
}
}
Our CommunityToolkit.Maui.Markup
package provides a much more concise way to use this converter in C#.
using CommunityToolkit.Maui.Markup;
class ColorToRgbaStringConverterPage : ContentPage
{
public ColorToRgbaStringConverterPage()
{
Content = new VerticalStackLayout
{
Children =
{
new Label()
.Text("My favourite Color is:"),
new Label()
.Bind(
Label.TextProperty,
static (ViewModel vm) => vm.MyFavoriteColor,
converter: new ColorToRgbaStringConverter())
}
};
}
}
You can find an example of this converter in action in the .NET MAUI Community Toolkit Sample Application.
You can find the source code for ColorToRgbaStringConverter
over on the .NET MAUI Community Toolkit GitHub repository.