我为WinForm UserControl创建了一个通用基类:
public partial class BaseUserControl<T> : UserControl
{
public virtual void MyMethod<T>()
{
// some base stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
以及基于以下内容的UserControl:
public partial class MyControl : BaseUserControl<SomeClass>
{
public override void MyMethod<SomeClass>()
{
// some specific stuff here
base.MyMethod<SomeClass>();
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但MyControl无法在VisualStudio Designer中进行编辑,因为它表示无法加载基类.我试图定义另一个类BaseUserControl,非泛型,希望它会加载它,但这个技巧似乎不起作用.
我已经有一个解决方法:定义一个接口,IMyInterface <T>,然后创建我的控件
public partial class MyControl : UserControl, IMyInterface<SomeClass>
Run Code Online (Sandbox Code Playgroud)
但是我失去了我的基本虚拟方法(不是很重要,但仍然......).
有没有办法为UserControl创建基础泛型类,有可能在VisualStudio Designer中编辑它?
我想定义以下控件:
public partial class ObjectSelectorControl<T> : UserControl where T : class
Run Code Online (Sandbox Code Playgroud)
问题是设计师无法解决这个问题.这个问题有解决方法吗?
我有一个winforms项目,我在程序集A上创建了一个类,它继承自System.Windows.Forms.Form作为我项目中各种表单的基类,基类类似于:
public partial class DataForm<T> : Form where T : class
{
T currentRecord;
protected T CurrentRecord
{
get
{
return currentRecord;
}
set
{
currentRecord = value;
CurrentRecordChanged();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我在程序集B上创建一个继承自我的DataForm的表单时,设计器将无法加载,但如果我编译它,应用程序运行正常.
表格如下:
public partial class SomeTableWindow : DataForm<SomeTable>
{
public SomeTableWindow ()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
The designer could not be shown for this file because none of the classes within it can be designed.
The designer inspected the following classes …Run Code Online (Sandbox Code Playgroud)