C # typed <T> usercontrol in development mode gives error
I have a custom class that comes from UserControl . The code:
public partial class Gallery<T> : UserControl where T : class, IElement, new() This class is how it should work. But when I try to enter the design mode of the form containing these Gallery classes, it gives me errors:
Could not find type 'PresentrBuilder.Forms.Gallery'. Make sure the assembly that contains this type. If this type is part of your development, make sure that the project is successfully embedded.
The variable 'pictureGallery' is either not declared or has never been assigned.
Note: ( pictureGallery actually has a Gallery<PictureElement> ).
How can this be solved? Thus, I cannot work in development mode, which makes it difficult to create my user interface.
The designer hates (i.e. does not support) the common controls, and this will not change soon, so do not do this. Instead, consider having a property (or the like) that accepts Type , and do some work at runtime (reflection, etc.) - or: don't use a constructor.
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 have ( new , IElement , etc.), but it just cannot perform the check at compile time.
Sometimes the easiest way to do this is to make an empty subclass that qualifies the general parameter.
This is often done using an ObservableCollection:
public class SomeItemCollection : ObservableCollection<SomeItem>{ } This is annoying, but it can solve your problems.
Like others, the Visual Studio developer has many problems handling generics in controls. I came across this myself, trying to implement something like a generic property view class.
The solution that worked for me determined the intermediate class, as Egor said. If I understand your question correctly, for your situation it should be something like this:
public class PictureElementGallery : Gallery<PictureElement> Then use the PictureElementGallery in your form, not the <PictureElement> gallery. The designer does not have to worry about this.
Instead of having a common control, control its interaction with a common class that is separate from the control itself. Then pass this class to the control.