DesignerSerializationVisibilityAttribute Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Specifica il tipo di persistenza da utilizzare durante la serializzazione di una proprietà in un componente in fase di progettazione.
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
- Ereditarietà
- Attributi
Esempio
Nell'esempio di codice seguente viene illustrato l'uso di un DesignerSerializationVisibilityAttribute set su Content. Rende persistenti i valori di una proprietà pubblica di un controllo utente, che può essere configurato in fase di progettazione. Per usare l'esempio, compilare prima di tutto il codice seguente in una libreria di controlli utente. Aggiungere quindi un riferimento al file di .dll compilato in un nuovo progetto di applicazione Windows. Se si usa Visual Studio, l'ContentSerializationExampleControl viene aggiunto automaticamente al Toolbox.
Trascinare il controllo da Toolbox a una maschera e impostare le proprietà dell'oggetto DimensionData elencato nel Finestra Proprietà. Quando si visualizza il codice per il modulo, il codice verrà aggiunto al InitializeComponent metodo del modulo padre. Questo codice imposta i valori delle proprietà del controllo su quelli impostati in modalità progettazione.
#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
Commenti
Quando un serializzatore mantiene lo stato persistente di un documento in modalità progettazione, spesso aggiunge codice al metodo di inizializzazione dei componenti per rendere persistenti i valori delle proprietà impostate in fase di progettazione. Ciò avviene per impostazione predefinita per la maggior parte dei tipi di base, se non è stato impostato alcun attributo per indirizzare altri comportamenti.
DesignerSerializationVisibilityAttributeCon , è possibile indicare se il valore di una proprietà è Visiblee deve essere salvato in modo permanente nel codice di inizializzazione, Hiddene non deve essere salvato in modo permanente nel codice di inizializzazione oppure è costituito da , che deve avere codice di Contentinizializzazione generato per ogni proprietà pubblica, non nascosta dell'oggetto assegnato alla proprietà .
I membri che non dispongono di un DesignerSerializationVisibilityAttribute oggetto verranno considerati come se abbiano un DesignerSerializationVisibilityAttribute oggetto con un valore .Visible I valori di una proprietà contrassegnata come Visible verranno serializzati, se possibile, da un serializzatore per il tipo. Per specificare la serializzazione personalizzata per un particolare tipo o proprietà, utilizzare .DesignerSerializerAttribute
Per altre informazioni, vedere Attributi.
Costruttori
| Nome | Descrizione |
|---|---|
| DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility) |
Inizializza una nuova istanza della DesignerSerializationVisibilityAttribute classe utilizzando il valore specificato DesignerSerializationVisibility . |
Campi
| Nome | Descrizione |
|---|---|
| Content |
Specifica che un serializzatore deve serializzare il contenuto della proprietà anziché la proprietà stessa. Il campo è di sola lettura. |
| Default |
Specifica il valore predefinito, ovvero Visible, ovvero una finestra di progettazione visiva usa le regole predefinite per generare il valore di una proprietà. Questo |
| Hidden |
Specifica che un serializzatore non deve serializzare il valore della proprietà. Questo |
| Visible |
Specifica che un serializzatore deve essere autorizzato a serializzare il valore della proprietà. Questo |
Proprietà
| Nome | Descrizione |
|---|---|
| TypeId |
Se implementato in una classe derivata, ottiene un identificatore univoco per questo Attribute. (Ereditato da Attribute) |
| Visibility |
Ottiene un valore che indica la modalità di serializzazione di base che deve essere utilizzata da un serializzatore per determinare se e come rendere persistente il valore di una proprietà. |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Indica se questa istanza e un oggetto specificato sono uguali. |
| GetHashCode() |
Restituisce il codice hash per questo oggetto. |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| IsDefaultAttribute() |
Ottiene un valore che indica se il valore corrente dell'attributo è il valore predefinito per l'attributo. |
| Match(Object) |
Quando sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza è uguale a un oggetto specificato. (Ereditato da Attribute) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |
Implementazioni dell'interfaccia esplicita
| Nome | Descrizione |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch. (Ereditato da Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Recupera le informazioni sul tipo per un oggetto, che può essere utilizzato per ottenere le informazioni sul tipo per un'interfaccia. (Ereditato da Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1). (Ereditato da Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Fornisce l'accesso alle proprietà e ai metodi esposti da un oggetto . (Ereditato da Attribute) |