DesignerSerializationVisibilityAttribute Třída
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje typ trvalosti, který se má použít při serializaci vlastnosti komponenty v době návrhu.
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
- Dědičnost
- Atributy
Příklady
Následující příklad kódu ukazuje použití DesignerSerializationVisibilityAttribute sady na Content. Zachová hodnoty veřejné vlastnosti uživatelského ovládacího prvku, který lze nakonfigurovat v době návrhu. Pokud chcete použít příklad, nejprve zkompilujte následující kód do knihovny uživatelských ovládacích prvků. Dále přidejte odkaz na zkompilovaný soubor .dll v novém projektu aplikace Windows. Pokud používáte Visual Studio, ContentSerializationExampleControl se automaticky přidá do pole Toolbox.
Přetáhněte ovládací prvek z Toolbox do formuláře a nastavte vlastnosti objektu DimensionData uvedený v okno Vlastnosti. Při zobrazení kódu formuláře bude kód přidán do InitializeComponent metody nadřazeného formuláře. Tento kód nastaví hodnoty vlastností ovládacího prvku na ty, které jste nastavili v režimu návrhu.
#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
Poznámky
Když serializátor zachovává trvalý stav dokumentu režimu návrhu, často přidává kód do inicializační metody komponent pro zachování hodnot vlastností, které byly nastaveny v době návrhu. K tomu dochází ve výchozím nastavení u většiny základních typů, pokud nebyl žádný atribut nastaven tak, aby směroval jiné chování.
DesignerSerializationVisibilityAttributePomocí , můžete určit, zda hodnota vlastnosti je Visiblea měla by být zachována v inicializačním kóduHidden, a neměli by být trvalé v inicializačním kódu, nebo se skládá z Content, který by měl mít inicializační kód vygenerovaný pro každou veřejnou, ne skrytou vlastnost objektu přiřazené k vlastnosti.
Členové, kteří nemají DesignerSerializationVisibilityAttribute , budou považováni za to, že mají DesignerSerializationVisibilityAttribute hodnotu Visible. Hodnoty vlastnosti označené jako Visible serializovány, pokud je to možné, serializátorem pro typ. Chcete-li zadat vlastní serializace pro určitý typ nebo vlastnost, použijte .DesignerSerializerAttribute
Další informace naleznete v tématu Atributy.
Konstruktory
| Name | Description |
|---|---|
| DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility) |
Inicializuje novou instanci DesignerSerializationVisibilityAttribute třídy pomocí zadané DesignerSerializationVisibility hodnoty. |
Pole
| Name | Description |
|---|---|
| Content |
Určuje, že serializátor by měl serializovat obsah vlastnosti, a ne samotnou vlastnost. Toto pole je jen pro čtení. |
| Default |
Určuje výchozí hodnotu, což je , to znamená Visible, vizuální návrhář používá výchozí pravidla k vygenerování hodnoty vlastnosti. Toto |
| Hidden |
Určuje, že serializátor by neměl serializovat hodnotu vlastnosti. Toto |
| Visible |
Určuje, že serializátor by měl být povolen serializovat hodnotu vlastnosti. Toto |
Vlastnosti
| Name | Description |
|---|---|
| TypeId |
Při implementaci v odvozené třídě získá jedinečný identifikátor pro tento Attribute. (Zděděno od Attribute) |
| Visibility |
Získá hodnotu označující základní serializační režim serializátor by měl použít při určování, zda a jak zachovat hodnotu vlastnosti. |
Metody
| Name | Description |
|---|---|
| Equals(Object) |
Určuje, zda tato instance a zadaný objekt jsou rovny. |
| GetHashCode() |
Vrátí kód hash pro tento objekt. |
| GetType() |
Získá Type aktuální instance. (Zděděno od Object) |
| IsDefaultAttribute() |
Získá hodnotu určující, zda aktuální hodnota atributu je výchozí hodnota atributu. |
| Match(Object) |
Při přepsání v odvozené třídě vrátí hodnotu, která určuje, zda se tato instance rovná zadanému objektu. (Zděděno od Attribute) |
| MemberwiseClone() |
Vytvoří mělkou kopii aktuálního Object. (Zděděno od Object) |
| ToString() |
Vrátí řetězec, který představuje aktuální objekt. (Zděděno od Object) |
Explicitní implementace rozhraní
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Mapuje sadu názvů na odpovídající sadu identifikátorů pro rozesílání. (Zděděno od Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Načte informace o typu objektu, který lze použít k získání informací o typu pro rozhraní. (Zděděno od Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Získá počet rozhraní typu informací, které objekt poskytuje (0 nebo 1). (Zděděno od Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Poskytuje přístup k vlastnostem a metodám vystaveným objektem. (Zděděno od Attribute) |