开发者

Descendant generic forms cannot display in the form designer!

I have a generic base form, and the descendant forms don't work in the designer. This is apparently a well-known problem, and the same answer is given here and here, to name just two places.

This solution seems to work for everyone else, and when I implement it, at least I get a different error:

"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MyBaseForm`1.resources" was correctly embedded or linked into assembly "MyAssembly" at compile time, or that all 开发者_如何转开发the satellite assemblies required are loadable and fully signed."

My classes are:

public partial class MyBaseForm<T> : Form { }

#if DEBUG
public partial class MyIntForm_Design : MyBaseForm<int> {
}
#endif

public partial class MyIntForm
#if DEBUG
: MyIntForm_Design {
#else
: MyBaseForm<int> {
#endif
}

Now what hoop do I have to jump through here?

EDIT: OMG, I found the problem - well, sort of. The base form has its Icon property set, which created something in the resource file. When I removed the icon and recompiled, the base form suddenly works!

Now answer credit for this question goes to whoever finds a workaround so that I can keep the icon in my base form!


Actually I found an easier workaround that doesn't risk being overwritten whenever you make cosmetic changes to the generic base form: just make the generic form inherit from another concrete form BaseForm, where the Icon property (and any other resources) are set.

Thus:

public partial class MyBaseForm<T> : BaseForm { }

Works a treat!


Carrying on from my comments, I managed to get this solution to work:

Fix embedded resources for a generic UserControl

What it appears to be doing is giving the resource a different name so as to remove the generic type, but associating the name with the correct type. In your example I did the following:

System.ComponentModel.ComponentResourceManager resources = new CustomComponentResourceManager(typeof(MyBaseForm<>), "MyBaseForm");

I put this in the InitializeComponent of the generic base control (I used a UserControl for my testing) and it fixed the designer of a concrete class. However, I still had to include the workaround of an intermediate class to specify a type for the generic argument.

This does not stop the designer from paving your change when it regenerates, and I know of no way to remove it from the designer code. The original poster of that link also has this problem. However, the quick fix in lieu of the benefits is not a bad trade-off so long as you can remember to do it!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜