Windows窗体通用继承 [英] Windows Forms Generic Inheritance

查看:162
本文介绍了Windows窗体通用继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些类:

  class Foo< T1,T2> :Form 
其中T1,T2:EventArgs

class MiddleGoo:Foo< X,Y>

class Goo:MiddleGoo

X,Y只是简单的类派生自EventArgs。



我在设计器中看到Goo,但是我想在这样的Foo和Goo之间创建一个类Boo:

  class Boo< T1,Y> :Foo< T1,Y> 
其中T1:EventArgs

类MiddleGoo:Boo< X,Y>

class Goo:MiddleGoo

中产阶级的解决方法无效,任何想法?编辑:我的意思是Y和X是像YEventArgs和XEventArgs的类,我的问题是关于看到设计器类Boo时,我将Y定义为T2,但仍然想要通过T1保持通用。



编辑2:我刚刚意识到我拼错了一些关于Y类的东西...

  public class Foo< T1,T2> :Form 
其中T1:EventArgs
其中T2:EventArgs
{
}

公共类Boo< T1> :Foo< T1,MyEventArgs2>
其中T1:EventArgs
{
}

公共类MiddleGoo:Boo< MyEventArgs1>
{
}

class Goo:MiddleGoo
{
}

公共类MyEventArgs2:EventArgs
{
}

public class MyEventArgs1:EventArgs
{
}

要清楚我在设计器中看不到Boo ......(我看不到MiddleGoo,但我不需要)

= VS2015.1



VS2015.1 ,Windows窗体设计器显示具有通用base classe的类没有任何问题。因此,VS的新版本不再需要其他文章中的解决方法,而且以下类将在设计器中显示,而不会出现任何问题。

因此,有一个基本的通用类如下:

  public class BaseForm< TModel,TService> :Form 
{
public TModel Model {get; set;}
public TService Service {get;设置;}
}

您可以在设计器中创建派生表单而没有任何问题:

  public class FooForm:BaseForm< Foo,FooService> 
{
}

旧版Visual Studio

因此,您可以在设计器中看到 BaseForm< T>:Form ,但 CategoryForm:BaseForm< Category> 不能在设计器中显示。作为这些情况下的解决方法,您应该创建一个 BaseCategoryForm:BaseForm< Category> ,然后显示 CategoryForm:BaseCategoryForm 在设计器中。



示例



假设这是接受TModel的基类如Model和TService as Service:

  public class BaseForm< TModel,TService> :Form 
{
public TModel Model {get; set;}
public TService Service {get; set;}
}

然后用这种方式创建一个中间表单, :

 公共类BaseFooForm:BaseForm< Foo,FooService> {} 
pre>

最终形式如下:

  public class FooForm :BaseFooForm 
{
}

现在最后的 FooForm 有设计师,您可以正常使用它。这样您就可以创建您的类,以便在设计器中获得支持。



注意

更新也适用于控制设计人员。因此,在 WinForm UserControl的泛型基类中,您不再需要VS> = VS2015的这种解决方法.1


I have these classes:

class Foo<T1, T2> : Form 
    where T1, T2 : EventArgs

class MiddleGoo : Foo<X,Y>

class Goo : MiddleGoo

X,Y are just simple classes derived from EventArgs.

I see Goo in designer, but i want to create a class Boo between Foo and Goo like this:

class Boo<T1, Y> : Foo<T1, Y>
where T1 : EventArgs

class MiddleGoo : Boo<X,Y>

class Goo : MiddleGoo

Workaround with middle class doesn't work, any ideas?

EDIT: I meant Y and X are classes like YEventArgs and XEventArgs and my problem is about seeing in designer class Boo when i defined Y as T2 but still want to keep it generic through T1.

EDIT2: I just realized I misspelled something about Y class...

public class Foo<T1, T2> : Form
    where T1 : EventArgs
    where T2 : EventArgs
{
}

public class Boo<T1> : Foo<T1, MyEventArgs2>
    where T1 : EventArgs
{
}

public class MiddleGoo : Boo<MyEventArgs1>
{
}

class Goo : MiddleGoo
{
}

public class MyEventArgs2 : EventArgs
{
}

public class MyEventArgs1 : EventArgs
{      
}

And to be clear I just can't see Boo in Designer... ( I can't see MiddleGoo too but i don't need to)

解决方案

For Visual Studio Version >= VS2015.1

Starting from VS2015.1, Windows Forms Designer shows classes which have generic base classe without any problem. So the workaround which is in other posts is no more required for newer versions of VS and the following class will be shown in designer without any problem.

So having a base generic class like this:

public class BaseForm<TModel,TService> : Form
{
    public TModel Model {get;set;}
    public TService Service {get; set;}
}

You can create the derived form without any problem in designer:

public class FooForm: BaseForm<Foo,FooService> 
{
}

Older Versions of Visual Studio

In older versions of Visual Studio, when the designer wants to host your form in designer, it tries to create an instance of base class of your form, and your class must have a non-generic base to so the designer can show it.

So you can see BaseForm<T>:Form can be shown in designer but CategoryForm:BaseForm<Category> can not be shown in designer. As a workaround in these cases you should create a BaseCategoryForm:BaseForm<Category> and then CategoryForm:BaseCategoryForm will be shown in designer.

Example

Suppose this is your base class that accepts TModel as Model and TService as Service for example:

public class BaseForm<TModel,TService> : Form
{
    public TModel Model {get;set;}
    public TService Service {get; set;}
}

Then create an intermediate form this way, with this line of code:

Public Class BaseFooForm: BaseForm<Foo, FooService>{ }

And the final form this way:

public class FooForm: BaseFooForm
{
}

Now the final FooForm has designer and you can work with it normally. This way you can create your classes to be supported in designer.

Note

The update is also applied on control designer. So also in generic base class for WinForm UserControl you no more need such workaround for VS>=VS2015.1 .

这篇关于Windows窗体通用继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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