天天看點

Xamarin.Forms 使用者界面——顔色 顔色 概觀 顔色 概要

顔色

PDF用于離線使用
  • 下載下傳PDF
示例代碼:
  • ColorsSample
  • 可綁定選擇器

讓我們知道你對此的感受

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

顯示為紅色,因為這是該裝置的使用者選擇的重音顔色 ; 該值根據使用者的偏好而改變。

Xamarin.Forms 使用者界面——顔色 顔色 概觀 顔色 概要

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中使用。

繼續閱讀