Visual Studio:设计一个派生自抽象基类的 UserControl 类

发布于 2024-08-30 22:19:22 字数 840 浏览 3 评论 0原文

我希望为我的一些自定义 UserControl 提供一个抽象基类。原因很明显:它们共享一些通用属性和方法(某些元素的基本实现)实际上是一个接口),我只想实现它们一次。

我通过定义我的抽象基类来完成此操作:

public abstract class ViewBase : UserControl, ISomeInterface

然后像往常一样,与设计器一起实现我的视图之一:

public partial class SpecialView : UserControl //all OK

到目前为止,一切都很好。现在,我将 SpecialView 类的派生替换为抽象基类:

public partial class SpecialView : ViewBase //disrupts the designer

现在,Visual Studio 2008 中的设计器将不再工作,并指出:设计器必须创建一个实例类型为“ViewBase”,但它不能,因为该类型被声明为抽象。

我怎样才能规避这个问题?我只是不想为所有内容复制相同的代码那些观点。

信息:有一个问题问题与虚拟方法,而不是抽象类,但没有适合我的解决方案。

I want to have an abstract base class for some of my custom UserControl's. The reason is obvious: they share some common properties and methods (a basic implementation of some elements of an interface actually), and I want to implement them only once.

I have done this by defining my abstract base class:

public abstract class ViewBase : UserControl, ISomeInterface

Then I went to implement one of my views, as usual, with the designer:

public partial class SpecialView : UserControl //all OK

Up to here all is fine. Now I replace the derivation of my SpecialView class with the abstract base class:

public partial class SpecialView : ViewBase //disrupts the designer

Now, the designer in Visual Studio 2008 won't work anymore, stating: The designer must create an instance of type 'ViewBase' but it cannot because the type is declared as abstract.

How can I circumvent this? I just do not want to have the same code copied for all those views.

Info: there is a question question with virtual methods, instead of abstract classes, but there is no suitable solution for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

路弥 2024-09-06 22:19:22

您可以将函数标记为虚拟并在继承类中重写它们,而不是使用抽象类

Instead of using abstract class, you can mark the functions virtual and override them in the inheriting classes

眼藏柔 2024-09-06 22:19:22

最好的解决方案在这里:

http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/

现在使用它,它很优雅,可以解决根本问题而不破坏你漂亮的 OOP 设计。

The best solution is here:

http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/

Using it now, it's elegant and gets around the underlying problem without breaking your nice OOP design.

梦过后 2024-09-06 22:19:22

尝试 Urban Potato 中的解决方案,该解决方案对我有用,奇怪的副作用我从未真正解释过,也从未得到一个好的解决方法。也许你会很幸运,不会有那种副作用!

Try this solution from Urban Potato, which worked for me, with a strange side effect that I never really had explained, and never got a good workaround. Maybe you'll get lucky and won't have that side-effect!

记忆で 2024-09-06 22:19:22

有人可能会说,就设计理念而言,期望能够在设计器中使用抽象控件是没有意义的。抽象类倾向于对一种对象进行建模,仅仅知道它是“X”并不能充分描述它 - 不存在抽象的鸟或汽车这样的东西,它总是特定类型的鸟或汽车。这样看,如果您想在设计器中查看自定义控件,它必须是一种特定类型的控件,而不是抽象控件,否则您在看什么呢?我可以理解为什么它很烦人,但我也可以理解为什么设计器以这种方式编码。

One could argue that it doesn't make sense in terms of design philosophy to expect to be able to work with an abstract control in the Designer. An abstract class tends to model a type of object for which simply knowing that it's an 'X' doesn't adequately describe it - there's no such thing as an abstract Bird or Car, it's always a specific type of bird or car. Looking at it this way, if you want to view a custom control in the designer, it has to be a specific type of control rather than an abstract one, otherwise what are you looking at? I can see why it's annoying, but I can also see why the Designer was coded in this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文