Xamarin.Forms 用户界面——颜色

颜色

PDF用于离线使用
示例代码:

让我们知道你对此的感受

Xamarin.Forms提供了一个灵活的跨平台Color类。

概观

本文介绍了Color在Xamarin.Forms中可以使用类的各种方法。

颜色

Color类提供了一些方法来建立一个颜色实例

  • 命名的颜色 -共同命名的颜色,包括收集RedGreenBlue
  • FromHex - 与HTML中使用的语法类似的字符串值,例如“00FF00”。可以将Alpha指定为第一对字符(“CC00FF00”)。
  • FromHsla - 色相,饱和度和亮度 double值,可选的alpha值(0.0-1.0)。
  • FromRgb - 红色,绿色和蓝色int值(0-255)。
  • FromRgba - 红,绿,蓝和alpha int值(0-255)。
  • FromUint - 设置一个double代表argb的值。

这里有一些示例颜色,分配给BackgroundColor使用不同变体的允许语法的一些标签:

var red    = new Label { Text = "Red",   BackgroundColor = Color.Red };
var orange = new Label { Text = "Orange",BackgroundColor = Color.FromHex("FF6A00") };
var yellow = new Label { Text = "Yellow",BackgroundColor = Color.FromHsla(0.167, 1.0, 0.5, 1.0) };
var green  = new Label { Text = "Green", BackgroundColor = Color.FromRgb (38, 127, 0) };
var blue   = new Label { Text = "Blue",  BackgroundColor = Color.FromRgba(0, 38, 255, 255) };
var indigo = new Label { Text = "Indigo",BackgroundColor = Color.FromRgb (0, 72, 255) };
var violet = new Label { Text = "Violet",BackgroundColor = Color.FromHsla(0.82, 1, 0.25, 1) };

var transparent = new Label { Text = "Transparent",BackgroundColor = Color.Transparent };
var @default = new Label    { Text = "Default",    BackgroundColor = Color.Default };
var accent = new Label      { Text = "Accent",     BackgroundColor = Color.Accent };

这些颜色显示在下面的每个平台上。注意最终颜色 - Accent- 是iOS和Android的蓝色颜色; 这个值由Xamarin.Forms定义。在Windows Phone上,Accent显示为红色,因为这是该设备的用户选择的重音颜色 ; 该值根据用户的偏好而改变。

彩色演示

Color.Default

使用将Default颜色值设置(或重新设置)回到平台默认值(了解每个属性在每个平台上表示不同的底层颜色)。

开发人员可以使用这个值来设置Color属性,但应该为它的分量RGB值查询此情况下(他们都设置为-1)。

Color.Transparent

将颜色设置为清除。

Color.Accent

在Windows Phone上,这是用户选择的补充颜色。良好的Windows Phone应用程序使用它作为其造型的一部分,以提供原生的外观和感觉。

在iOS和Android上,此实例设置为默认背景上可见的对比色,但与默认文字颜色不同。

附加方法

Color 实例包括可用于创建新颜色的其他方法:

  • AddLuminosity - 通过根据提供的增量修改亮度来返回新颜色。
  • WithHue - 返回一个新的颜色,用提供的值替换色调。
  • WithLuminosity - 返回一种新的颜色,用所提供的值替换亮度。
  • WithSaturation - 返回一个新的颜色,用提供的值代替饱和度。
  • MultiplyAlpha - 通过修改alpha来返回一个新的颜色,将其乘以所提供的alpha值。

Device.OnPlatform

此代码段用于Device.OnPlatform选择性地设置ActivityIndicatoriOS上的颜色:

ActivityIndicator activityIndicator = new ActivityIndicator {
    Color = Device.OnPlatform(Color.Black, Color.Default, Color.Default),
    IsRunning = true
};

从XAML使用

颜色也可以使用定义的颜色名称或这里显示的十六进制表示在XAML中轻松引用:

<Label Text="Sea color" BackgroundColor="Aqua" />
<Label Text="RGB" BackgroundColor="#00FF00" />
<Label Text="Alpha plus RGB" BackgroundColor="#CC00FF00" />
<Label Text="Tiny RGB" BackgroundColor="#0F0" />
<Label Text="Tiny Alpha plus RGB" BackgroundColor="#C0F0" />

概要

Xamarin.Forms Color类用于创建平台感知颜色引用。它可以在共享代码和XAML中使用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值