简体   繁体   English

C#类型 <T> 设计模式下的用户控制出现错误

[英]C# typed <T> usercontrol in design mode gives error

I've got a custom class, which derives from UserControl . 我有一个自定义类,该类派生自UserControl The code: 代码:

public partial class Gallery<T> : UserControl where T : class, IElement, new()

This classworks like it's supposed to work. 这堂课就像它应该工作一样。 But, when I try to enter design mode of the form which contains these Gallery classes, it gives me errors: 但是,当我尝试进入包含这些Gallery类的表单的设计模式时,出现了以下错误:

  • Could not find type 'PresentrBuilder.Forms.Gallery'. 找不到类型“ PresentrBuilder.Forms.Gallery”。 Please make sure that the assembly that contains this type is referenced. 请确保引用了包含此类型的程序集。 If this type is a part of your development project, make sure that the project has been successfully built. 如果此类型是开发项目的一部分,请确保已成功构建该项目。

  • The variable 'pictureGallery' is either undeclared or was never assigned. 变量“ pictureGallery”未声明或从未分配。

Note: ( pictureGallery actually is a Gallery<PictureElement> ). 注意:( pictureGallery实际上是Gallery<PictureElement> )。

How can solve this? 怎么解决呢? This way, I can't work in design mode which makes creating my userinterface quite hard. 这样,我无法在设计模式下工作,这会使创建用户界面非常困难。

The designer hates (ie doesn't support) generic controls, and that isn't going to change any time soon, so don't do that. 设计者讨厌(即不支持)通用控件,并且这种情况不会很快改变,因此不要这样做。 Instead, consider having a property (or similar) that accepts a Type , and do some work at runtime (reflection etc) - or: don't use the designer. 相反,考虑拥有一个接受Type的属性(或类似属性),并在运行时(反射等)做一些工作-或:不要使用设计器。

For example, if you have: 例如,如果您有:

public Type ControlType {get;set;} // comparable to T in the original

You can use: 您可以使用:

IElement el = (IElement) Activator.CreateInstance(ControlType);

This will give you everything you currently have ( new , IElement , etc) - but it just can't do any validation at compile-time. 这将为您提供当前拥有的所有内容( newIElement等)-但在编译时无法进行任何验证。

Sometimes the easiest thing to do in this case is to make an empty subclass that qualifies the generic parameter. 有时,在这种情况下最容易做的事情是制作一个空的子类来限定通用参数。

This is often done with the ObservableCollection: 这通常是通过ObservableCollection完成的:

public class SomeItemCollection : ObservableCollection<SomeItem>{

}

It is kind of irritating, but it may solve your problems. 这有点烦人,但可以解决您的问题。

Like the others have stated, the Visual Studio Designer has a lot of trouble handling generics in controls. 就像其他人所说的那样,Visual Studio Designer在处理控件中的泛型方面有很多麻烦。 I've run into this myself when trying to implement something like a generic 'property viewer' class. 我在尝试实现类似通用的“属性查看器”类时遇到了这种情况。

The solution that worked for me was defining an intermediary class, like Egor said. 对我有用的解决方案是定义一个中间类,如Egor所说。 If I understand your question correctly, for your situation, that should be something like this: 如果我正确理解了您的问题,对于您的情况,应该是这样的:

public class PictureElementGallery : Gallery<PictureElement>

Then use the PictureElementGallery on your form, instead of Gallery < PictureElement > . 然后,在表单上使用PictureElementGallery而不是Gallery <PictureElement> The designer should have no trouble with that. 设计师对此应该没有任何问题。

Instead of having a generic control, have the control interact with a generic class that is separate from the control itself. 代替具有通用控件,使控件与与控件本身分离的通用类进行交互。 Then pass this class into the control. 然后将此类传递到控件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM