共用方式為


DesignerSerializationVisibilityAttribute 類別

定義

指定在設計時間串行化元件上的屬性時要使用的持續性類型。

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
屬性

範例

以下程式碼範例示範了如何將集合 DesignerSerializationVisibilityAttribute 映射到 Content。 它持續保存使用者控制項的公開屬性值,該屬性可在設計時設定。 舉例來說,首先將以下程式碼編譯成使用者控制函式庫。 接著,新增一個已編譯的 .dll 檔案參考,並建立一個新的 Windows Application 專案。 如果你使用 Visual Studio,ContentSerializationExampleControl 會自動加入 Toolbox

將控制項從 Toolbox拖曳到一個表單,並設定屬性視窗中列出的 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.ComponentModel;
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 : UserControl
{
    Container components;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public DimensionData Dimensions => new(this);

    public ContentSerializationExampleControl() => InitializeComponent();

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            components?.Dispose();
        }
        base.Dispose(disposing);
    }

    void InitializeComponent() => components = new Container();
}

[TypeConverter(typeof(ExpandableObjectConverter))]
// This attribute indicates that the public properties of this object should be listed in the property grid.
public class DimensionData
{
    readonly 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 => owner.Location;
        set => owner.Location = value;
    }

    public Size FormSize
    {
        get => 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.

如需詳細資訊,請參閱屬性

建構函式

名稱 Description
DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility)

使用指定的DesignerSerializationVisibilityAttribute值初始化類別的新實例DesignerSerializationVisibility

欄位

名稱 Description
Content

規定序列化器應該序列化屬性的內容,而非屬性本身。 此欄位是唯讀的。

Default

指定預設值,即 Visible視覺設計師使用預設規則來產生屬性的值。 此 static 欄位為唯讀。

Hidden

規定序列化器不應序列化屬性的值。 此 static 欄位為唯讀。

Visible

規定序列化器應允許序列化該屬性的值。 此 static 欄位為唯讀。

屬性

名稱 Description
TypeId

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

(繼承來源 Attribute)
Visibility

會取得一個值,指示序列化器在判斷是否以及如何持久保存屬性值時,應該使用的基本序列化模式。

方法

名稱 Description
Equals(Object)

指出這個實例和指定的物件是否相等。

GetHashCode()

傳回這個物件的哈希碼。

GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

會取得一個值,表示該屬性目前值是否為預設值。

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)

適用於

另請參閱