我想要一个摘要UserControl
,BaseControl
,它实现了一个接口IBaseControl
。 然而,设置类抽象休息VisualStudio的设计(这是与Visual Studio(例如,参见this StackOverflow posting获取更多信息),而据我所知,有no change expected in the near futureVisualStudio 2010 Designer引发实现的虚拟方法
于是,一个已知的问题来解决这个问题,我让BaseControl
不是抽象的,其实现的IBaseControl
方法虚拟然而,由于这些方法并没有什么意义了BaseControl
(例如,不是所有组件都已经被添加),我让他们扔:
public class BaseControl : UserControl, IBaseControl
{
/// <summary>
/// This IBaseControl method is not abstract because
/// that breaks the Designer
/// </summary>
public virtual void LoadSettings()
{
throw new NotImplementedException("Implement in derived class.");
}
private void BaseControl_Load(object sender, EventArgs e)
{
// intention: derived methods automagically load their settings
this.LoadSettings();
}
}
在导出的控件,我有相应的覆盖:
public partial class DerivedControl : BaseControl
{
public override void LoadSettings()
{
// load settings
}
}
尽管如此,当我尝试在设计器中打开控件时,出现一个错误,指示BaseControl.LoadSettings
引发了异常。
现在,请记住LoadSettings
是在基类中调用的,所以当Designer加载DerivedControl
时,它依次调用BaseControl
的加载方法,该方法会抛出。
您是否遇到过类似问题?你怎么处理这件事?如果可能的话,我想有一个优雅的解决方案。
没有repro,我没有想到一个。现在工具栏上有两个控件,不要选错了。你应该重写OnLoad而不是使用Load事件。 – 2011-06-09 20:53:54