我为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中编辑它?
我正在为WinForms项目组装一个轻量级的MVP模式.一切都编译好,运行良好.但是,当我尝试在Visual Studio中以设计模式打开WinForm时,出现" 路径中的非法字符 "错误.我的WinForm使用泛型并从基类Form类继承.在WinForm中使用泛型是否有问题?
这是WinForm和基本Form类.
public partial class TapsForm : MvpForm<TapsPresenter, TapsFormModel>, ITapsView
{
public TapsForm()
{
InitializeComponent();
}
public TapsForm(TapsPresenter presenter)
:base(presenter)
{
InitializeComponent();
UpdateModel();
}
public IList<Taps> Taps
{
set { gridTaps.DataSource = value; }
}
private void UpdateModel()
{
Model.RideId = Int32.Parse(cboRide.Text);
Model.Latitude = Double.Parse(txtLatitude.Text);
Model.Longitude = Double.Parse(txtLongitude.Text);
}
}
Run Code Online (Sandbox Code Playgroud)
基本形式MvpForm:
public class MvpForm<TPresenter, TModel> : Form, IView
where TPresenter : class, IPresenter
where TModel : class, new()
{
private readonly TPresenter presenter;
private TModel model;
public …Run Code Online (Sandbox Code Playgroud)