abstract class CustomControl : UserControl
{
protected abstract int DoStuff();
}
class DetailControl : CustomControl
{
protected override int DoStuff()
{
// do stuff
return result;
}
}
我在表单中放置了一个DetailControl。它在运行时正确呈现,但设计器显示错误,并且不会打开,因为基本用户控件是抽象的。Visual Studio设计器中的抽象UserControl继承
目前,我正在考虑下面的补丁,这对我来说似乎很不对劲,因为我希望子类被强制实施该方法。
class CustomControl : UserControl
{
protected virtual int DoStuff()
{
throw new InvalidOperationException("This method must be overriden.");
}
}
class DetailControl : CustomControl
{
protected override int DoStuff()
{
// do stuff
return result;
}
}
任何人有更好的主意,如何解决这个问题?
我最近也有类似的,去了有一个单独的接口,提供我想要的方式。当我需要编辑它时,我也尝试将基类更改为UserControl,但它很混乱。 – Deanna
[我如何才能让Visual Studio 2008 Windows窗体设计器渲染实现抽象基类的窗体?](http://stackoverflow.com/questions/1620847/how-can-i-get-visual- studio-2008-windows-forms-designer-to-render-a-form-that-im) – Breeze