zoukankan      html  css  js  c++  java
  • 如何消除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

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

  • 相关阅读:
    浙江省新一代多普勒天气雷达系统
    删除目录下的所有文件及子文件夹
    在南京14所测试出厂雷达(转)
    c++实现aes加密算法,对字符串进行加密
    自已在别人基础上封装的AES数法 C++
    IOS发布问题
    GameCenter 使用指南
    [iOS]AES加密在iOS上面的实现
    【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012625日更新iap恢复详解】
    基于cocos2dx引擎的游戏框架设计
  • 原文地址:https://www.cnblogs.com/ServiceboyNew/p/2705738.html
Copyright © 2011-2022 走看看