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

查看:23
本文介绍了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"的实例,但它不能,因为该类型被声明为抽象.

我该如何规避?我只是不想为所有这些视图复制相同的代码.

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.

推荐答案

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

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

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

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