DesignerSerializationVisibilityAttribute 类

指定在设计时序列化组件上的属性 (Property) 时所使用的持久性类型。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.Method Or AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Event)> _
Public NotInheritable Class DesignerSerializationVisibilityAttribute
    Inherits Attribute
用法
Dim instance As DesignerSerializationVisibilityAttribute
[AttributeUsageAttribute(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event)] 
public sealed class DesignerSerializationVisibilityAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Method|AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Event)] 
public ref class DesignerSerializationVisibilityAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event) */ 
public final class DesignerSerializationVisibilityAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Event) 
public final class DesignerSerializationVisibilityAttribute extends Attribute

备注

当序列化程序保持设计模式文档的可持续状态时,它通常会向组件的初始化方法中添加代码,以便保持已在设计时设置的属性 (Property) 值。如果尚未设置指示其他行为的属性 (Attribute),大多数基类型都会默认出现此情况。

DesignerSerializationVisibilityAttribute 允许您指示属性 (Property) 值是否为 Visible 而应在初始化代码中保持,是否为 Hidden 而不应在初始化代码中保持,或者是否由 Content 组成而应为分配给该属性的对象的每个公共属性(而非隐藏属性)生成初始化代码。

没有 DesignerSerializationVisibilityAttribute 的成员将被视为具有值为 VisibleDesignerSerializationVisibilityAttribute。如果可能,序列化程序会将标记为 Visible 的属性 (Property) 值序列化为该类型。要为特定类型或属性 (Property) 指定自定义序列化,请使用 DesignerSerializerAttribute

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

示例

下面的代码示例演示如何将 DesignerSerializationVisibilityAttribute 集用于 Content。它保持用户控件的公共属性 (Property) 值,这些值可在设计时配置。要使用该示例,请先将以下代码编译到用户控件库中。然后,在新的 Windows 应用程序项目中添加对编译的 .dll 文件的引用。如果您使用的是 Visual Studio,ContentSerializationExampleControl 会自动添加到“工具箱”。

将控件从“工具箱”拖动到窗体,并设置“属性”窗口中列出的 DimensionData 对象的属性 (Property)。在查看窗体的代码时,代码应已添加到了父窗体的 InitializeComponent 方法中。此代码将控件的属性值设置为在设计模式中设置的属性 (Property) 值。

Imports System
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 'New


        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub 'Dispose


        Private Sub InitializeComponent()
        End Sub 'InitializeComponent
    End Class 'ContentSerializationExampleControl

    ' 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 'New


        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 'DimensionData
End Namespace 'DesignerSerializationVisibilityTest
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;
        }
    }
    }
}
#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;
   }
};
package DesignerSerializationVisibilityTest;

import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.Drawing.*;
import System.Windows.Forms.*;

// 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
   extends System.Windows.Forms.UserControl
{
    private System.ComponentModel.Container components = null;

    /** @attribute DesignerSerializationVisibility(
         DesignerSerializationVisibility.Content)
     */
    /** @property 
     */
    public DimensionData get_Dimensions()
    {
        return new DimensionData(this);
    } //get_Dimensions

    public ContentSerializationExampleControl()
    {
        InitializeComponent();
    } //ContentSerializationExampleControl

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose

    private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
    } //InitializeComponent
} //ContentSerializationExampleControl

/** @attribute TypeConverterAttribute(System.ComponentModel.
     ExpandableObjectConverter.class)
 */
public class DimensionData
{
    // This attribute indicates that the public properties of this 
    // object should be listed in the property grid.
    private Control owner;

    // This class reads and writes the Location and Size properties 
    // from the Control which it is initialized to.
    DimensionData(Control owner)
    {
        this.owner = owner;
    } //DimensionData

    /** @property 
     */
    public Point get_Location()
    {
        return owner.get_Location();
    } //get_Location

    /** @property 
     */
    public void set_Location(Point value)
    {
        owner.set_Location(value);
    } //set_Location

    /** @property 
     */
    public Size get_FormSize()
    {
        return owner.get_Size();
    } //get_FormSize

    /** @property 
     */
    public void set_FormSize(Size value)
    {
        owner.set_Size(value);
    } //set_FormSize
} //DimensionData

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.DesignerSerializationVisibilityAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

DesignerSerializationVisibilityAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor
AttributeCollection 类
PropertyDescriptorCollection

其他资源

如何:使用 DesignerSerializationVisibilityAttribute 序列化标准类型的集合