DesignerSerializationVisibilityAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定於設計階段序列化元件上屬性時所使用的保存 (Persistence) 類型。
public ref class DesignerSerializationVisibilityAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property)]
public sealed class DesignerSerializationVisibilityAttribute : Attribute
public sealed class DesignerSerializationVisibilityAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property)]
public sealed class DesignerSerializationVisibilityAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Event | System.AttributeTargets.Field | System.AttributeTargets.Method | System.AttributeTargets.Property)>]
type DesignerSerializationVisibilityAttribute = class
inherit Attribute
type DesignerSerializationVisibilityAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Method | System.AttributeTargets.Property)>]
type DesignerSerializationVisibilityAttribute = class
inherit Attribute
Public NotInheritable Class DesignerSerializationVisibilityAttribute
Inherits Attribute
- 繼承
- 屬性
範例
下列程式代碼範例示範 DesignerSerializationVisibilityAttribute 如何使用設定為 Content。 它會保存使用者控制件的公用屬性值,可在設計時間進行設定。 若要使用範例,請先將下列程式代碼編譯為使用者控件連結庫。 接下來,在新的 Windows 應用程式專案中,新增已編譯 .dll 檔案的參考。 如果您使用 Visual Studio,會自動 ContentSerializationExampleControl
將 新增至 [工具箱]。
將控件從 [工具箱] 拖曳至窗體,並設定 屬性視窗 中列出的物件屬性DimensionData
。 當您檢視表單的程式代碼時,程式代碼會新增至 InitializeComponent
父表單的方法。 此程式代碼會將控件屬性的值設定為您在設計模式中設定的值。
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Windows::Forms;
// This attribute indicates that the public properties of this object should be listed in the property grid.
[TypeConverterAttribute(System::ComponentModel::ExpandableObjectConverter::typeid)]
public ref class DimensionData
{
private:
Control^ owner;
internal:
// This class reads and writes the Location and Size properties from the Control which it is initialized to.
DimensionData( Control^ owner )
{
this->owner = owner;
}
public:
property Point Location
{
Point get()
{
return owner->Location;
}
void set( Point value )
{
owner->Location = value;
}
}
property Size FormSize
{
Size get()
{
return owner->Size;
}
void set( Size value )
{
owner->Size = value;
}
}
};
// The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility
// attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.
// The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
// for the class object. Content persistence will not work for structs without a custom TypeConverter.
public ref class ContentSerializationExampleControl: public System::Windows::Forms::UserControl
{
private:
System::ComponentModel::Container^ components;
public:
property DimensionData^ Dimensions
{
[DesignerSerializationVisibility(DesignerSerializationVisibility::Content)]
DimensionData^ get()
{
return gcnew DimensionData( this );
}
}
ContentSerializationExampleControl()
{
InitializeComponent();
}
public:
~ContentSerializationExampleControl()
{
if ( components != nullptr )
{
delete components;
}
}
private:
void InitializeComponent()
{
components = gcnew System::ComponentModel::Container;
}
};
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
namespace DesignerSerializationVisibilityTest
{
// The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility
// attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.
// The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
// for the class object. Content persistence will not work for structs without a custom TypeConverter.
public class ContentSerializationExampleControl : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DimensionData Dimensions
{
get
{
return new DimensionData(this);
}
}
public ContentSerializationExampleControl()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
}
[TypeConverterAttribute(typeof(System.ComponentModel.ExpandableObjectConverter))]
// This attribute indicates that the public properties of this object should be listed in the property grid.
public class DimensionData
{
private Control owner;
// This class reads and writes the Location and Size properties from the Control which it is initialized to.
internal DimensionData(Control owner)
{
this.owner = owner;
}
public Point Location
{
get
{
return owner.Location;
}
set
{
owner.Location = value;
}
}
public Size FormSize
{
get
{
return owner.Size;
}
set
{
owner.Size = value;
}
}
}
}
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms
Namespace DesignerSerializationVisibilityTest
_
' The code for this user control declares a public property of type DimensionData with a DesignerSerializationVisibility
' attribute set to DesignerSerializationVisibility.Content, indicating that the properties of the object should be serialized.
' The public, not hidden properties of the object that are set at design time will be persisted in the initialization code
' for the class object. Content persistence will not work for structs without a custom TypeConverter.
Public Class ContentSerializationExampleControl
Inherits System.Windows.Forms.UserControl
Private components As System.ComponentModel.Container = Nothing
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Dimensions() As DimensionData
Get
Return New DimensionData(Me)
End Get
End Property
Public Sub New()
InitializeComponent()
End Sub
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If (components IsNot Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
End Sub
End Class
' This attribute indicates that the public properties of this object should be listed in the property grid.
<TypeConverterAttribute(GetType(System.ComponentModel.ExpandableObjectConverter))> _
Public Class DimensionData
Private owner As Control
' This class reads and writes the Location and Size properties from the Control which it is initialized to.
Friend Sub New(ByVal owner As Control)
Me.owner = owner
End Sub
Public Property Location() As Point
Get
Return owner.Location
End Get
Set(ByVal Value As Point)
owner.Location = Value
End Set
End Property
Public Property FormSize() As Size
Get
Return owner.Size
End Get
Set(ByVal Value As Size)
owner.Size = Value
End Set
End Property
End Class
End Namespace 'DesignerSerializationVisibilityTest
備註
當串行化程式保存設計模式檔的可保存狀態時,通常會將程式代碼新增至元件的初始化方法,以保存在設計時間設定的屬性值。 如果尚未將任何屬性設定為導向其他行為,則預設會針對大部分的基本類型進行這項操作。
DesignerSerializationVisibilityAttribute使用 時,您可以指出屬性的值是否為 Visible,而且應該保存在初始化程式代碼中,Hidden而且不應該保存在初始化程式代碼中,或是由 所組成Content,其應該為每個公用的公用產生初始化程序代碼,而不是指派給 屬性之物件的隱藏屬性。
沒有 DesignerSerializationVisibilityAttribute 的成員會被視為其 DesignerSerializationVisibilityAttribute 值為 的 Visible。 如果可能,標示為 Visible 的屬性值會由型別的串行化程式串行化。 若要指定特定類型或屬性的自訂串列化,請使用 DesignerSerializerAttribute。
如需詳細資訊,請參閱屬性。
建構函式
DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility) |
使用指定的 DesignerSerializationVisibility 值,初始化 DesignerSerializationVisibilityAttribute 類別的新執行個體。 |
欄位
Content |
指定序列化程式應該序列化屬性的內容,而不是屬性本身。 此欄位為唯讀。 |
Default |
指定預設值 Visible,亦即,視覺化設計工具使用預設規則產生屬性值。 這個 |
Hidden |
指定序化程式不應該序列化屬性的值。 這個 |
Visible |
指定序列化程式應該要允許序列化屬性的值。 這個 |
屬性
TypeId |
在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。 (繼承來源 Attribute) |
Visibility |
在決定是否要或是如何保存屬性的值時,取得代表序列化程式所應使用基本序列化模式的值。 |
方法
Equals(Object) |
指示這個執行個體和指定的物件是否相等。 |
GetHashCode() |
傳回此物件的雜湊碼。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsDefaultAttribute() |
取得值,指出目前屬性值是否為屬性的預設值。 |
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) |