【问题标题】:Design Mode Preprocessor Directive Workaround设计模式预处理器指令解决方法
【发布时间】:2011-10-03 18:40:50
【问题描述】:

我知道没有 DESIGN、DESIGN_MODE、DESIGN_TIME 等预处理器指令值。但是,我需要一些可以解决问题的东西。我不能使用普通的 If 语句,因为在我的情况下,我需要更改继承的类,以便控件在设计时正确呈现。如果不是,我会收到一个异常,因为继承的类是一个抽象类。

这是我想要完成的任务:

Partial Class MyCustomControl
#If DesignMode Then
       Inherits UserControl
#Else
    Inherits WidgetControl
#End If

有什么建议吗?

【问题讨论】:

  • 你无法完成这项工作。您必须使您的基类成为非抽象类以支持设计时。
  • 但我确信有一种方法可以绕过它而无需非抽象类。我什至尝试创建一个继承自 WidgetControl 的中间类,但是当我调试和/或发布时,我想使用 WidgetControl。必须有一种使用预处理器指令的方法。

标签: vb.net winforms visual-studio preprocessor design-time


【解决方案1】:

在过去,我创建了一个虚拟类作为过渡。有时 VS 仍然会弄清楚你在做什么并感到不安,但通常重启 IDE 会解决这个问题。

Partial Class MyCustomControl : MyAbstractClass_FAKE_IMPL
{
  //your normal class
}

Partial Class MyAbstractClass_FAKE_IMPL : MyAbstractClass
{

  //let IDE autogenerate implementation code that you are always going to override in reality.

}

【讨论】:

    【解决方案2】:

    尝试使用:

    if (this.DesignMode == true)
    {    }
    else
    {    }
    

    【讨论】:

    • “this”指的是继承自 UserControl 的当前类。
    • 如何将它与继承一起使用?我很确定我不能。
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多