Export (0) Print
Expand All

Walkthrough: Implementing a New Style

For most ASP.NET mobile controls, the Style base class provides enough coverage for property styles. However, individual controls might require specialized style classes that provide additional properties specific to the control.

All specialized style classes need to inherit from the Style base class. The base class provides automatic support for state management and inheritance. To use these features, the specialized style class must define and store all new properties in a property provided by the base class.

To add support for properties in a specialized style class

  1. Write a specialized style class that inherits from the Style base class.

  2. Override the CreateStyle method of the control, and return an instance of the specialized style class.

    protected override Style CreateStyle()
    {
        return new NewStyle();
    }
    
  3. Optionally, create a new property on the control that strongly types the Style property. This new property can be used in the following step (step 4).

    protected NewStyle NewStyle
    {
        get
        {
            return (NewStyle)Style;
        }
    }
    
  4. For each new property in the specialized class, provide a property in the control class itself. These properties will be the public accessors for the style properties. The implementations of these properties can call the style object.

    protected String ThemeName
    {
        get
        {
            return NewStyle.ThemeName;
        }
        set
        {
            NewStyle.ThemeName = value;
        }
    }
    

To add a new style

  1. Declare a public static string as a member field, which is initialized to the return value of a call to the RegisterStyle method defined in the base class. The following code example creates a string property that supports inheritance and whose default value is the empty string ("").

    public static String ThemeNameKey = 
        RegisterStyle(ThemeName, typeof(String), String.Empty, true);
    

    The RegisterStyle method registers the new style property and returns a unique key that can be used to refer to the style in the property. The parameters to the RegisterStyle method define the name, data type, and default value of the property, as well as its inheritance behavior.

    The key should be public because control adapters also use it to access the inheritance-aware value of the property.

  2. Using the default indexer property of the base class, create a public property whose implementation accesses the property.

    public String ThemeName
    {
        get
        {
            return (String)this[ThemeNameKey];
        }
        set
        {
            this[ThemeNameKey] = value;
        }
    }
    

Community Additions

Show:
© 2014 Microsoft