AmbientValueAttribute 類別

定義

指定要傳遞至屬性的值,讓屬性從其他來源取得其值。 這稱為「環境」。 此類別無法獲得繼承。

public ref class AmbientValueAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class AmbientValueAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type AmbientValueAttribute = class
    inherit Attribute
Public NotInheritable Class AmbientValueAttribute
Inherits Attribute
繼承
AmbientValueAttribute
屬性

範例

下列程式碼範例示範如何使用 AmbientValueAttribute 來強制執行名為 AlertForeColor 之屬性的環境行為。 如需完整的程式代碼清單,請參閱如何:在Windows Forms控制項中套用屬性

[AmbientValue(typeof(Color), "Empty")]
[Category("Appearance")]
[DefaultValue(typeof(Color), "White")]
[Description("The color used for painting alert text.")]
public Color AlertForeColor
{
    get
    {
        if (this.alertForeColorValue == Color.Empty &&
            this.Parent != null)
        {
            return Parent.ForeColor;
        }

        return this.alertForeColorValue;
    }

    set
    {
        this.alertForeColorValue = value;
    }
}

// This method is used by designers to enable resetting the
// property to its default value.
public void ResetAlertForeColor()
{
    this.AlertForeColor = AttributesDemoControl.defaultAlertForeColorValue;
}

// This method indicates to designers whether the property
// value is different from the ambient value, in which case
// the designer should persist the value.
private bool ShouldSerializeAlertForeColor()
{
    return (this.alertForeColorValue != AttributesDemoControl.ambientColorValue);
}
<AmbientValue(GetType(Color), "Empty"), _
Category("Appearance"), _
DefaultValue(GetType(Color), "White"), _
Description("The color used for painting alert text.")> _
Public Property AlertForeColor() As Color
    Get
        If Me.alertForeColorValue = Color.Empty AndAlso (Me.Parent IsNot Nothing) Then
            Return Parent.ForeColor
        End If

        Return Me.alertForeColorValue
    End Get

    Set(ByVal value As Color)
        Me.alertForeColorValue = value
    End Set
End Property

' This method is used by designers to enable resetting the
' property to its default value.
Public Sub ResetAlertForeColor()
    Me.AlertForeColor = AttributesDemoControl.defaultAlertForeColorValue
End Sub

' This method indicates to designers whether the property
' value is different from the ambient value, in which case
' the designer should persist the value.
Private Function ShouldSerializeAlertForeColor() As Boolean
    Return Me.alertForeColorValue <> AttributesDemoControl.ambientColorValue
End Function

備註

如果控制項上的屬性具有環境行為,則此屬性必須存在。 環境屬性會查詢其父代的值,例如 Control.Font 屬性或 Control.BackColor 屬性。

一般而言,視覺化設計工具會 AmbientValueAttribute 使用 屬性來決定要保留給屬性的值。 這通常是一個值,會導致屬性從另一個來源取得其值。 環境值的範例是 Color.Empty 做為 屬性的環境 BackColor 值。 如果您在表單上有控制項, BackColor 且控制項的 屬性設定為與表單的 屬性不同的色彩 BackColor ,您可以將 BackColor 控制項的 屬性設定 BackColorColor.Empty 為 表單的 屬性。

建構函式

AmbientValueAttribute(Boolean)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為 Boolean 值。

AmbientValueAttribute(Byte)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為 8 位元不帶正負號的整數 (Unsigned Integer)。

AmbientValueAttribute(Char)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為 Unicode 字元。

AmbientValueAttribute(Double)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為雙精度浮點數。

AmbientValueAttribute(Int16)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為 16 位元帶正負號的整數。

AmbientValueAttribute(Int32)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為 32 位元帶正負號的整數。

AmbientValueAttribute(Int64)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為 64 位元帶正負號的整數。

AmbientValueAttribute(Object)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為物件。

AmbientValueAttribute(Single)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為單精確度浮點數。

AmbientValueAttribute(String)

初始化 AmbientValueAttribute 類別的新執行個體,需指定其值為字串。

AmbientValueAttribute(Type, String)

初始化 AmbientValueAttribute 類別的新執行個體,需指定值和型別。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
Value

取得物件,其為這個 AmbientValueAttribute 的值。

方法

Equals(Object)

判斷指定的 AmbientValueAttribute 是否等於目前的 AmbientValueAttribute

GetHashCode()

傳回這個執行個體的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於