DesignerSerializationVisibilityAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定在设计时序列化组件上的属性时要使用的持久性类型。
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会自动添加到 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 将被视为具有 DesignerSerializationVisibilityAttribute 价值 Visible的成员。 标记的属性的值 Visible 将按类型序列化程序进行序列化(如果可能)。 若要为特定类型或属性指定自定义序列化,请使用 DesignerSerializerAttribute.
有关更多信息,请参阅特性。
构造函数
| 名称 | 说明 |
|---|---|
| DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility) |
使用指定的DesignerSerializationVisibilityAttribute值初始化类的新实例DesignerSerializationVisibility。 |
字段
| 名称 | 说明 |
|---|---|
| Content |
指定序列化程序应序列化属性的内容,而不是属性本身。 此字段是只读的。 |
| Default |
指定默认值,即 Visible视觉设计器使用默认规则生成属性的值。 此 |
| Hidden |
指定序列化程序不应序列化属性的值。 此 |
| Visible |
指定应允许序列化程序序列化属性的值。 此 |
属性
| 名称 | 说明 |
|---|---|
| TypeId |
在派生类中实现时,获取此 Attribute的唯一标识符。 (继承自 Attribute) |
| Visibility |
获取一个值,该值指示序列化程序在确定是否以及如何保留属性的值时应使用的基本序列化模式。 |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object) |
指示此实例和指定对象是否相等。 |
| GetHashCode() |
返回此对象的哈希代码。 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| IsDefaultAttribute() |
获取一个值,该值指示属性的当前值是否为属性的默认值。 |
| Match(Object) |
在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。 (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
显式接口实现
| 名称 | 说明 |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,该信息可用于获取接口的类型信息。 (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对对象公开的属性和方法的访问。 (继承自 Attribute) |