DefaultSettingValueAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定應用程式設定屬性的預設值。
public ref class DefaultSettingValueAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class DefaultSettingValueAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type DefaultSettingValueAttribute = class
inherit Attribute
Public NotInheritable Class DefaultSettingValueAttribute
Inherits Attribute
- 繼承
- 屬性
範例
以下程式碼範例展示了 DefaultSettingValueAttribute 將 應用於包裝類別四個屬性 FormSettings 中的三個,而包裝類別是從該 ApplicationSettingsBase 類別衍生出來的。 此類別用於持久化形式的位置、大小、背景顏色及文字。 前三個形式屬性都有預設值。
完整程式碼範例列於 ApplicationSettingsBase 課程總覽中。
//Application settings wrapper class
ref class FormSettings sealed: public ApplicationSettingsBase
{
public:
[UserScopedSettingAttribute()]
property String^ FormText
{
String^ get()
{
return (String^)this["FormText"];
}
void set( String^ value )
{
this["FormText"] = value;
}
}
public:
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("0, 0")]
property Point FormLocation
{
Point get()
{
return (Point)(this["FormLocation"]);
}
void set( Point value )
{
this["FormLocation"] = value;
}
}
public:
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("225, 200")]
property Size FormSize
{
Size get()
{
return (Size)this["FormSize"];
}
void set( Size value )
{
this["FormSize"] = value;
}
}
public:
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("LightGray")]
property Color FormBackColor
{
Color get()
{
return (Color)this["FormBackColor"];
}
void set(Color value)
{
this["FormBackColor"] = value;
}
}
};
//Application settings wrapper class
sealed class FormSettings : ApplicationSettingsBase
{
[UserScopedSettingAttribute()]
public String FormText
{
get { return (String)this["FormText"]; }
set { this["FormText"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("0, 0")]
public Point FormLocation
{
get { return (Point)(this["FormLocation"]); }
set { this["FormLocation"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("225, 200")]
public Size FormSize
{
get { return (Size)this["FormSize"]; }
set { this["FormSize"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("LightGray")]
public Color FormBackColor
{
get { return (Color)this["FormBackColor"]; }
set { this["FormBackColor"] = value; }
}
}
'Application settings wrapper class. This class defines the settings we intend to use in our application.
NotInheritable Class FormSettings
Inherits ApplicationSettingsBase
<UserScopedSettingAttribute()>
Public Property FormText() As String
Get
Return CStr(Me("FormText"))
End Get
Set(ByVal value As String)
Me("FormText") = value
End Set
End Property
<UserScopedSettingAttribute(), DefaultSettingValueAttribute("0, 0")>
Public Property FormLocation() As Point
Get
Return CType(Me("FormLocation"), Point)
End Get
Set(ByVal value As Point)
Me("FormLocation") = value
End Set
End Property
<UserScopedSettingAttribute(), DefaultSettingValueAttribute("225, 200")>
Public Property FormSize() As Size
Get
Return CType(Me("FormSize"), Size)
End Get
Set(ByVal value As Size)
Me("FormSize") = value
End Set
End Property
<UserScopedSettingAttribute(), DefaultSettingValueAttribute("LightGray")>
Public Property FormBackColor() As Color
Get
Return CType(Me("FormBackColor"), Color)
End Get
Set(ByVal value As Color)
Me("FormBackColor") = value
End Set
End Property
End Class
備註
Important
DefaultSettingValueAttribute 只能套用到個別設定屬性;將此屬性套用於整個應用程式設定類別是無效的。
不同環境的供應商對使用這些設備 DefaultSettingValueAttribute的要求或限制可能不同。 例如,不 LocalFileSettingsProvider 要求此屬性,若資料庫中已有預設值或使用者修改值,該屬性會覆寫該屬性所提供的任何值。
DefaultSettingValueAttribute 要求預設值能以字串表示。 因此,使用 XML 序列化的設定無法透過此屬性指定預設值。 有些提供者可能會選擇支援多種序列化方案,這些方案可在編譯時使用 SettingsSerializeAsAttribute。
Caution
此屬性指定的預設值會以純文字形式儲存在編譯後的 .exe 或 .dll 檔案中。 因此,這些預設值本質上是不安全的。
建構函式
| 名稱 | Description |
|---|---|
| DefaultSettingValueAttribute(String) |
初始化該 DefaultSettingValueAttribute 類別的實例。 |
屬性
| 名稱 | Description |
|---|---|
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
| Value |
會取得應用程式設定屬性的預設值。 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _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) |
適用於
另請參閱
- SettingsProvider
- LocalFileSettingsProvider
- SettingsSerializeAsAttribute
- 應用程式設定:Windows Forms