如何消除Web自定义控件的“自生成”复合属性的冗余类名称?

这个标题有些长,可能一时半会儿无法看懂。不如我们边看代码边说:

[C#]

namespace ServerControlTest
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Point
    {
        [NotifyParentProperty(true)]
        public double X { get; set; }
        [NotifyParentProperty(true)]
        public double Y { get; set; }
        [NotifyParentProperty(true)]
        public double Z { get; set; }

        public Point()
        {

        }
        public Point(double x,double y,double z)
        {
            X = x;
            Y = y;
            Z = z;
        }
    }

    [ParseChildren(true), PersistChildren(false)]
    public class PointCollection:Control
    {
        private Point _point = null;

        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [NotifyParentProperty(true)]
        public Point Point
        {
            get
            {
                if (_point == null)
                {
                    _point = new Point(0,0,0);
                }
                return _point;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                writer.Write("[X=0,Y=0,Z=0]");
            }
            else
            {
                writer.Write(string.Format("[{0},{1},{2}]", _point.X, _point.Y, _point.Z));
            }
        }

    }
}

[VB.NET]

Namespace ServerControlTest
    <TypeConverter(GetType(ExpandableObjectConverter))> _
    Public Class Point
        <NotifyParentProperty(True)> _
        Public Property X() As Double
            Get
                Return m_X
            End Get
            Set
                m_X = Value
            End Set
        End Property
        Private m_X As Double
        <NotifyParentProperty(True)> _
        Public Property Y() As Double
            Get
                Return m_Y
            End Get
            Set
                m_Y = Value
            End Set
        End Property
        Private m_Y As Double
        <NotifyParentProperty(True)> _
        Public Property Z() As Double
            Get
                Return m_Z
            End Get
            Set
                m_Z = Value
            End Set
        End Property
        Private m_Z As Double


        Public Sub New()
        End Sub
        Public Sub New(x__1 As Double, y__2 As Double, z__3 As Double)
            X = x__1
            Y = y__2
            Z = z__3
        End Sub
    End Class

    <ParseChildren(True), PersistChildren(False)> _
    Public Class PointCollection
        Inherits Control
        Private _point As Point = Nothing

        <PersistenceMode(PersistenceMode.InnerProperty)> _
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        <NotifyParentProperty(True)> _
        Public ReadOnly Property Point() As Point
            Get
                If _point Is Nothing Then
                    _point = New Point(0, 0, 0)
                End If
                Return _point
            End Get
        End Property

        Protected Overrides Sub Render(writer As HtmlTextWriter)
            If DesignMode Then
                writer.Write("[X=0,Y=0,Z=0]")
            Else
                writer.Write(String.Format("[{0},{1},{2}]", _point.X, _point.Y, _point.Z))
            End If
        End Sub

    End Class
End Namespace

这段代码是一个简单定义的显示X,Y,Z的输出控件。当拖拽到aspx页面上请注意属性框中:

Oh my GOD!蓝色部分那是什么?我不需要啊!你看一个标准的控件哪有那么难看的?如下截图是一个标准的Button——注意其复合属性:

看看从WebControl继承的属性Font,多么干净!

我们可以对这些讨厌的字符串进行阉割么?答案是——当然可以!敏感的你或许已经发现“Point”旁边的那个字符串就是默认输出Point类自身的那个ToString方法!那么如果我们在Point类中重写ToString呢?譬如:

[C#]

 public override string ToString()
        {
            return string.Empty;
        }

[VB.NET]

Public Overrides Function ToString() As String
    Return String.Empty
End Function

运行一下,啊哈哈!成功啦!

posted @ 2012-09-27 15:49  Serviceboy  阅读(1077)  评论(0编辑  收藏  举报