简体   繁体   English

如何隐藏ASP.NET自定义控件的继承属性?

[英]How can I hide inherited properties of an ASP.NET custom control?

Here is my first experience creating a custom control. 这是我第一次创建自定义控件的经验。 My real example is much larger, but this is boiled down for clarity. 我的实际示例更大,但为了清晰起见,将其简化了下来。 Ultimately, I need to hide as many properties of the custom control as possible so that when I share my new control with the rest of my team, they need only worry about the few properties that are required. 最终,我需要隐藏尽可能多的自定义控件属性,以便当我与团队其他成员共享新控件时,他们只需要担心所需的少数属性。

I have a control called TimeNow which inherits System.Web.UI.WebControls.Literal and basically just prints the current time on the web page: 我有一个名为TimeNow的控件,该控件继承了System.Web.UI.WebControls.Literal并且基本上只在网页上显示当前时间:

public class TimeNow : Literal

// Set to private so Text is hidden from the editor.
private string Text
{
    get;
    set;
}

protected override void Render(HtmlTextWriter writer)
{
    // Get and write the time of now.
}

This works , but it seems clunky. 可行 ,但似乎很笨拙。 I no longer see Text available in intellisense when I drop the control on a web page, but I do receive a warning that my Text is hiding the inherited Text. 当我将控件放在网页上时,我不再在智能感知中看到可用的文本,但是我确实收到一条警告,提示我的文本正在隐藏继承的文本。 Is there a better way to hide the Text property? 有没有更好的方法来隐藏Text属性?

There should be more to that warning message, suggesting you use the new keyword if you really intend to hide the inherited member, do what it says: 该警告消息应该有更多内容,建议如果您确实打算隐藏继承的成员,请使用new关键字,按照提示进行操作:

public class TimeNow : Literal
{
    new private string Text
    {
        get;
        set;
    }
}

Try this: 尝试这个:

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
private string Text
{

}

I think you're doing something wrong if you derive from a Literal that behaves like an ITextControl (Literal has implemented this Interface) and then you try to remove the essential Text property? 我认为如果您从行为类似于ITextControlLiteral派生ITextControl (文字已实现了此接口),然后尝试删除基本的Text属性? This is like deriving from cat but don't want to let them do "meow" and force them flying like a duck. 这就像衍生自猫,但又不想让它们做“喵”声并强迫它们像鸭子一样飞翔。 You are searching for the Animal class instead, I think. 我想,您正在搜索的是Animal类。

I don't know much about ASP.NET (only .Net for the Desktop). 我对ASP.NET(台式机仅.NET)了解甚少。 Maybe it is possible to use composition instead of inheritance (if you not really need the Literal -> "Cat") and you can inherit from System.Web.UI.Control (-> "Animal") instead. 也许可以使用合成代替继承(如果您确实不需要Literal->“ Cat”),则可以从System.Web.UI.Control (->“ Animal”)继承。

public class TimeNow : System.Web.UI.Control
{
   // no need to do something here with the Text property, is not defined
}

or with composition 或组成

public class TimeNow : System.Web.UI.Control
{
     private readonly Literal literal;

     public TimeNow()
     {
         this.literal = new Literal();
         // ... and set the Text etc., no one else can access
     }

     // or something like this

     public TimeNow(ILiteralFactory literalFactory)
     {
         // the factory is just an example... don't know your context but this way your newly created literal can't be accessed
         this.literal = literalFactory.CreateNewLiteral();
         // do what you want with the text control, e.g. store internally
         // Clone() the Literal etc.
         // the
     }
}

Update: Had a very quick look into the MSDN and maybe a Content Control is the thing you are looking for, instead of the Literal. 更新:对MSDN进行了快速浏览,也许您正在寻找内容控件而不是文字控件 (sorry if wrong, writing Desktop Apps) (对不起,如果写错了,请编写桌面应用程序)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM