LinearGradientBrush 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
用線性漸層畫出一個區域。
public ref class LinearGradientBrush sealed : GradientBrush
/// [Windows.Foundation.Metadata.Activatable(Microsoft.UI.Xaml.Media.ILinearGradientBrushFactory, 65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class LinearGradientBrush final : GradientBrush
[Windows.Foundation.Metadata.Activatable(typeof(Microsoft.UI.Xaml.Media.ILinearGradientBrushFactory), 65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class LinearGradientBrush : GradientBrush
Public NotInheritable Class LinearGradientBrush
Inherits GradientBrush
<LinearGradientBrush ...>
oneOrMoreGradientStops
</LinearGradientBrush
- 繼承
- 屬性
範例
這個範例會產生一個帶有四種顏色的線性漸層,並用它來繪製一個 矩形。
<StackPanel>
<!-- This rectangle is painted with a vertical linear gradient. -->
<Rectangle Width="200" Height="100">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1.0" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</StackPanel>
備註
LinearGradientBrush 是一種筆 刷 ,用於許多可能的 UI 屬性,這些屬性會用 Brush 來填充物件在應用程式介面中部分或全部的視覺區域。 使用 筆刷 值的最常用屬性範例包括: Control.Background、 Control.Foreground、 Shape.Fill、 Control.BorderBrush、 Panel.Background、 TextBlock.Foreground。 LinearGradientBrush 是較常用的 SolidColorBrush 類型的替代方案。
LinearGradientBrush 的 起點 與 終點 屬性描述了相對座標空間中的兩個點。 這會為梯度產生方向,通常會指定水平梯度或垂直梯度。 也可以使用斜坡。 LinearGradientBrush 通常在 GradientStops 屬性(有序集合)中包含兩個或以上的 GradientStop 值。 每個漸層停止指定一種顏色和一個偏移。 Offset 代表在漸層上介於 0( 起點)到1( 終點)之間的位置,而筆刷的實際像素長度和漸層會根據你套用 LinearGradientBrush 的值來調整。 關於 Offset 值的定義以及 Offset、 StartPoint 和 EndPoint 之間的關聯,請參見 XAML 筆刷。 這是常見的使用
你可以用 Transparent 值來設定 GradientStop 的其中一個顏色。 雖然這不會在視覺上對使用者介面有任何變化(因為它是透明的),但這點在測試時是可以偵測到的。 欲了解更多點擊測試資訊,請參閱 滑鼠互動中的「點擊測試」章節。
LinearGradientBrush 的 GradientStop 值可以作為轉場或裝飾動畫的一部分來動畫化。 使用可以動畫 色彩值的 專用動畫類型。 這通常涉及必須 .(GradientStop.Color) 參與一段較長的屬性路徑,以達成 Storyboard.TargetProperty 價值。 關於屬性目標設定以及如何動畫使用 筆刷 值的物件,請參見 分鏡動畫。
筆刷作為 XAML 資源
XAML 中可宣告的每種 筆刷 類型(SolidColorBrush、LinearGradientBrush、 ImageBrush)都是設計成資源,讓你能在整個應用程式中重複使用該筆刷作為資源。 筆 刷 類型所展示的 XAML 語法適合用來定義筆刷作為資源。 當你宣告筆刷為資源時,還需要一個 x:Key 屬性 ,之後用它來參考其他 UI 定義中的該資源。 欲了解更多 XAML 資源及如何使用 x:Key 屬性,請參閱 ResourceDictionary 與 XAML 資源參考。
將筆刷宣告為資源的優點是減少了構建 UI 所需的執行時物件數量:筆刷現在作為共用資源,為物件圖的多個部分提供值。
如果你查看現有 Windows 執行階段 XAML 控制項的控制範本定義,你會發現這些範本大量使用筆刷資源(雖然通常是 SolidColorBrush,而非 LinearGradientBrush)。 這些資源中有許多是系統資源,它們使用 {ThemeResource} 標記擴充功能 作為資源參考,而非 {StaticResource} 標記擴充功能。 欲了解更多如何在你自己的控制模板 XAML 中使用系統資源筆刷的資訊,請參閱 XAML 主題資源。
建構函式
| 名稱 | Description |
|---|---|
| LinearGradientBrush() |
初始化 LinearGradientBrush 類別的新實例。 |
| LinearGradientBrush(GradientStopCollection, Double) |
初始化一個新的 LinearGradientBrush 類別實例,該類別具有指定的 GradientStopCollection 與角度。 |
屬性
| 名稱 | Description |
|---|---|
| ColorInterpolationMode |
取得或設定一個 ColorInterpolationMode 的列舉值,指定漸層顏色如何插值。 (繼承來源 GradientBrush) |
| Dispatcher |
它總是在 Windows 應用程式 SDK 應用程式中回傳 |
| DispatcherQueue |
得到 |
| EndPoint |
取得或設定線性梯度的終點二維座標。 |
| EndPointProperty |
識別 EndPoint 依賴性質。 |
| GradientStops |
可以設定或設定筆刷的漸層停止。 (繼承來源 GradientBrush) |
| MappingMode |
取得或設定一個 BrushMappingMode 的列舉值,指定梯度刷子的位置座標是絕對座標還是相對於輸出區域。 (繼承來源 GradientBrush) |
| Opacity |
取得或設定 筆刷的不透明度程度。 (繼承來源 Brush) |
| RelativeTransform |
取得或設定使用相對座標套用到筆刷上的轉換。 (繼承來源 Brush) |
| SpreadMethod |
取得或設定一種擴散方法,指定如何繪製一個從要繪製物件範圍內開始或結束的漸層。 (繼承來源 GradientBrush) |
| StartPoint |
取得或設定線性梯度的起始二維座標。 |
| StartPointProperty |
識別 StartPoint 依賴性屬性。 |
| Transform |
取得或設定套用到畫筆上的轉換。 (繼承來源 Brush) |
方法
| 名稱 | Description |
|---|---|
| ClearValue(DependencyProperty) |
清除依賴性財產的局部價值。 (繼承來源 DependencyObject) |
| GetAnimationBaseValue(DependencyProperty) |
回傳任何為相依屬性建立的基礎值,適用於動畫未啟用時。 (繼承來源 DependencyObject) |
| GetValue(DependencyProperty) |
回傳 DependencyObject 中相依屬性的當前有效值。 (繼承來源 DependencyObject) |
| PopulatePropertyInfo(String, AnimationPropertyInfo) |
定義了一個可以被動畫化的屬性。 (繼承來源 Brush) |
| PopulatePropertyInfoOverride(String, AnimationPropertyInfo) |
當在派生類別中覆寫時,定義了一個可被動畫化的屬性。 (繼承來源 Brush) |
| ReadLocalValue(DependencyProperty) |
若設定了本地值,則回傳依賴性質的局部值。 (繼承來源 DependencyObject) |
| RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback) |
註冊一個通知函式,用於監聽此 DependencyObject 實例中特定 DependencyProperty 的變更。 (繼承來源 DependencyObject) |
| SetValue(DependencyProperty, Object) |
將相依屬性的局部值設定在 DependencyObject 上。 (繼承來源 DependencyObject) |
| UnregisterPropertyChangedCallback(DependencyProperty, Int64) |
取消先前透過呼叫 RegisterPropertyChangedCallback 註冊的變更通知。 (繼承來源 DependencyObject) |