颜色
-
PDF用于离线使用
- 示例代码:
让我们知道你对此的感受
Xamarin.Forms提供了一个灵活的跨平台Color类。
概观
本文介绍了Color
在Xamarin.Forms中可以使用类的各种方法。
颜色
本Color
类提供了一些方法来建立一个颜色实例
- 命名的颜色 -共同命名的颜色,包括收集
Red
,Green
和Blue
。 - 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
选择性地设置ActivityIndicator
iOS上的颜色:
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中使用。