Code Serialization


Whereas the DefaultEvent and DefaultProperty attributes affect only the behavior of the Properties window, the DefaultValue attribute serves a dual purpose: It also plays a role in helping the Windows Forms Designer determine which code is serialized to InitializeComponent. Properties that don't have a default value are automatically included in InitializeComponent, but those that do are included only if their configured values differ from their default values. To avoid unnecessarily changing a property, you should set your initial property values to match the value set by the DefaultValue attribute.

The DesignerSerializationVisibility attribute is another attribute that affects the code serialization process, as determined by the DesignerSerializationVisibility enumeration value passed to its constructor:

namespace System.ComponentModel {   enum DesignerSerializationVisibility {     Hidden = 0, // Don't initialize property     Visible = 1, // Initialize property if nondefault value (default)     Content = 2 // Initialize sets of properties on a subobject   } }


The default, Visible, causes a property's value to be set in InitializeComponent if the value of the property is not the same as the value of the default. But if you prefer that no code be generated to initialize a propertyyou might prefer this for a property like IsAlarmSet, which is useful only at run timeuse Hidden:

[DesignerSerializationVisibility(    DesignerSerializationVisibility.Hidden)] public bool IsAlarmSet {   get {...}   set {...} }


You can use Hidden in conjunction with the Browsable attribute set to false for run-time-only properties. Although the Browsable attribute determines whether a property is visible in the Properties window, its value may still be serialized unless you prevent that by using Hidden.

By default, properties that maintain a collection of custom types cannot be serialized to code. Such a property is implemented by the clock control in the form of a "messages to self" feature, which captures a set of messages and displays them at the appropriate date and time. To enable serialization of a collection, you can apply DesignerSerializationVisibility.Content to instruct the Windows Forms Designer to walk into the property and serialize its internal structure:

[Category("Behavior")]  [Description("Stuff to remember for later.")]  [DesignerSerializationVisibility(   DesignerSerializationVisibility.Content)] public Collection<MessageToSelf> MessagesToSelf {   get {...}   set {...} }


The generated InitializeComponent code for a single message looks like this:

// AlarmClockControl.Designer.cs partial class AlarmClockControl {   ...   void InitializeComponent() {     ...    this.alarmClockControl.MessagesToSelf.Add(      new AlarmClockControlLibrary.MessageToSelf(       new System.DateTime(2005, 12, 8, 21, 59, 23, 577),        "My First Message"));     ...   } }


This code also needs a translator class to help the Windows Forms Designer serialize the proper code to construct a MessageToSelf type. This is covered in detail in the section "Type Converters" later in this chapter.




Windows Forms 2.0 Programming
Windows Forms 2.0 Programming (Microsoft .NET Development Series)
ISBN: 0321267966
EAN: 2147483647
Year: 2006
Pages: 216

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net