ColorToCmykStringConverter

ColorToCmykStringConverter 是一个单向转换器,允许用户将 Color 值绑定转换为其 CMYK string 等效项,格式为:CMYK(蓝绿色, 品红色,黄色,键),其中蓝绿色、品红色、黄色和键的值介于 0% 和 100% 之间(例如,CMYK(0%,100%,100%,0%) 表示 Colors.Red

Convert 方法将提供的 Colorvalue 转换为其 CMYK string 等效项,然后返回。

不支持 ConvertBack 方法。

BaseConverter 属性

以下属性是在基类 public abstract class BaseConverter 中实现的:

properties 说明
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。

ICommunityToolkitValueConverter 属性

以下属性在 public interface ICommunityToolkitValueConverter 中实现:

properties 类型​​ 描述
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。
DefaultConvertBackReturnValue object? IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) 引发 Exception 时返回的默认值。 当 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters 设置为 true 时,将使用此值。

语法

以下示例演示如何使用 ColorToCmykStringConverter 显示特定 Color 的 CMYK 等效字符串。

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>

使用 ColorToCmykStringConverter

ColorToCmykStringConverter 可以在 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.ColorToCmykStringConverterPage">

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

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

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

</ContentPage>

C#

ColorToCmykStringConverter 可在 C# 中按如下所示方式使用:

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

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

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

C# 标记

我们的 CommunityToolkit.Maui.Markup 包提供了在 C# 中使用此转换器的更简洁的方法。

using CommunityToolkit.Maui.Markup;

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

示例

可以在 .NET MAUI 社区工具包示例应用程序中找到此转换器的示例。

API

可以在 .NET MAUI 社区工具包 GitHub 存储库查看ColorToCmykStringConverter 的源代码