LinearGradientBrush 類別

定義

使用線形漸層繪製區域。

public ref class LinearGradientBrush sealed : GradientBrush
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Xaml.Media.ILinearGradientBrushFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 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.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Xaml.Media.ILinearGradientBrushFactory, 65536, "Windows.Foundation.UniversalApiContract")]
class LinearGradientBrush final : GradientBrush
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Xaml.Media.ILinearGradientBrushFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class LinearGradientBrush : GradientBrush
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Xaml.Media.ILinearGradientBrushFactory), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class LinearGradientBrush : GradientBrush
Public NotInheritable Class LinearGradientBrush
Inherits GradientBrush
<LinearGradientBrush ...>
  oneOrMoreGradientStops
</LinearGradientBrush
繼承
Object Platform::Object IInspectable DependencyObject Brush GradientBrush LinearGradientBrush
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

以下範例建立一個四色線性漸層,並用它來繪製 Rectangle

<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 是一種 Brush 類型,用於許多可能的 UI 屬性,這些屬性會使用 Brush 填滿應用程式 UI 中的部分或所有物件的視覺區域。 一些最常使用 Brush 值的屬性範例包括 :Control.BackgroundControl.ForegroundShape.FillControl.BorderBrushPanel.BackgroundTextBlock.Foreground。 LinearGradientBrush 是較常用 SolidColorBrush 類型的替代方案。

LinearGradientBrush 的 StartPointEndPoint 屬性描述相對座標空間中的兩個點。 這會建立漸層的方向,通常這會指定水準漸層或垂直漸層。 也可以使用對角漸層。 LinearGradientBrush 通常會有兩個或多個GradientStops屬性的GradientStop值, (已排序的集合) 。 每個 GradientStop 都會指定 ColorOffsetOffset 代表 0 (StartPoint) 和 1 (EndPoint) 沿著漸層的位置,而筆刷及其漸層的實際圖元長度會根據套用 LinearGradientBrush 作為值的 UI 進行調整。 如需如何定義 Offset 值以及如何 與 OffsetStartPointEndPoint 相關的詳細資訊,請參閱 使用筆刷。 常用的用法

您可以使用其中一個GradientStop色彩的Transparent值。 雖然這不會以視覺化方式將任何變更套用至 UI, (它是透明的) ,但該點可偵測到以進行點擊測試。 如需點擊測試的詳細資訊,請參閱 滑鼠互動的一節。

LinearGradientBrush 的 GradientStop 值可以動畫呈現為轉換或裝飾動畫的一部分。 使用其中一種可讓 Color 值產生動畫效果的專用動畫類型。 這通常牽 .(GradientStop.Color) 涉到 為 Storyboard.TargetProperty 值較長屬性路徑的一部分。 如需屬性目標的詳細資訊,以及如何以動畫顯示使用 Brush 值的屬性,請參閱 分鏡腳本動畫

XAML 資源形式的筆刷

您可以在 XAML (SolidColorBrush、LinearGradientBrushImageBrush) 中宣告的每個Brush類型都是要定義為資源,以便在整個應用程式中重複使用該筆刷作為資源。 針對 Brush類型顯示的 XAML 語法適用于將筆刷定義為資源。 當您將筆刷宣告為資源時,您也需要 x:Key 屬性 ,稍後您將用來從其他 UI 定義參考該資源。 如需 XAML 資源及如何使用 x:Key 屬性的詳細資訊,請參閱 ResourceDictionary 和 XAML 資源參考

將筆刷宣告為資源的優點是可減少建構 UI 所需的執行時間物件數目:筆刷現在會共用為物件圖形的多個部分提供值的通用資源。

如果您查看Windows 執行階段 XAML 控制項的現有控制項範本定義,您會看到範本會廣泛地使用筆刷資源 (雖然這些通常是SolidColorBrush,而不是 LinearGradientBrush) 。 其中許多資源都是系統資源,而且會針對資源參考使用 {ThemeResource} 標記延伸 ,而不是 {StaticResource} 標記延伸。 如需如何在您自己的控制項範本 XAML 中使用系統資源筆刷的詳細資訊,請參閱 XAML 主題資源

建構函式

LinearGradientBrush()

初始化 LinearGradientBrush 類別的新實例。

LinearGradientBrush(GradientStopCollection, Double)

初始化 LinearGradientBrush 類別的新實例,這個實例具有指定的 GradientStopCollection 和 angle。

屬性

ColorInterpolationMode

取得或設定 ColorInterpolationMode 列舉值,指定漸層色彩的插補方式。

(繼承來源 GradientBrush)
Dispatcher

取得這個 物件相關聯的 CoreDispatcherCoreDispatcher代表可在 UI 執行緒上存取DependencyObject的功能,即使程式碼是由非 UI 執行緒起始也一樣。

(繼承來源 DependencyObject)
EndPoint

取得或設定線性漸層的結束二維座標。

EndPointProperty

識別 EndPoint 相依性屬性。

GradientStops

取得或設定筆刷的漸層停駐點。

(繼承來源 GradientBrush)
MappingMode

取得或設定 BrushMappingMode 列舉值,指定漸層筆刷的位置座標是絕對還是相對於輸出區域。

(繼承來源 GradientBrush)
Opacity

取得或設定 Brush的不透明度。

(繼承來源 Brush)
RelativeTransform

取得或設定使用相對座標套用到筆刷的轉換。

(繼承來源 Brush)
SpreadMethod

取得或設定展開方法的類型,該類型指定如何繪製在所要繪製物件之邊界內開始或結束的漸層。

(繼承來源 GradientBrush)
StartPoint

取得或設定線性漸層的起始二維座標。

StartPointProperty

識別 StartPoint 相依性屬性。

Transform

取得或設定套用到筆刷的轉換。

(繼承來源 Brush)

方法

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)

適用於

另請參閱