Visual Studio:设计一个从抽象基类派生的UserControl类 [英] Visual Studio: Design a UserControl class that derives from an abstract base class

查看:139
本文介绍了Visual Studio:设计一个从抽象基类派生的UserControl类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

现在, Visual Studio 2008将不再起作用,并指出:设计者必须创建类型为'ViewBase'的实例,但不能,因为该类型被声明为抽象类型。

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.

推荐答案

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

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

这篇关于Visual Studio:设计一个从抽象基类派生的UserControl类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆