通用基类WinForm的用户控件 [英] Generic base class for WinForm UserControl

查看:116
本文介绍了通用基类WinForm的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个通用的基类,一个WinForm用户控件:

I created a generic base class for a WinForm UserControl:

public partial class BaseUserControl<T> : UserControl
{
    public virtual void MyMethod<T>() 
    { 
        // some base stuff here 
    }
}

和基于一个用户控件:

public partial class MyControl : BaseUserControl<SomeClass>
{
    public override void MyMethod<SomeClass>() 
    { 
        // some specific stuff here 
        base.MyMethod<SomeClass>();
    }
}

它工作正常,但MyControl不能在VisualStudio的设计进行编辑,因为它说,它无法加载基类。
我试着来定义另一个类BaseUserControl,非通用的,希望它会加载它,但诀窍似乎并没有工作。

It works fine, but MyControl cannot be edited in the VisualStudio Designer, because it says it cannot load the base class. I tried to define another class BaseUserControl, non generic, hoping it would load it, but the trick doesn't seem to work.

我已经有一个解决方法:定义一个接口,IMyInterface的&LT; T&gt;和然后创建我的控制,

I already have a workaround: define an interface, IMyInterface<T>, and then create my control as

public partial class MyControl : UserControl, IMyInterface<SomeClass>

不过,我失去了我的基本虚拟方法(而不是什么大不了的事,但仍...)。

But I lose my base virtual methods (not a big deal, but still...).

有没有一种方法来创建一个用户控件基础通用类,用方法可行的设计的VisualStudio来编辑它?

Is there a way to create a base generic class for a UserControl, with the possiblity to edit it in the VisualStudio Designer?

推荐答案

我们正在做同样的事情,我们通过专业一类一解决,并从专业类派生。
从你的例子使用code,这意味着类似:

We're doing the same thing and we work around by specializing a class first and derive from the specialized class. Using the code from your example this means something like:

public partial class UserControl : UserControlDesignable 
{

...
}
public class UserControlDesignable : BaseUserControl<Someclass> { }

设计者还在演戏片状有时 - 但大部分的它工作时

The designer is still acting flaky sometimes - but most of the time it works.

这篇关于通用基类WinForm的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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