汎用フォームの継承時にデザイナーに表示できません.神馬ですか?

4245 ワード

今日は変な問題にぶつかって、自分で2時間もやってもうまくいかなかった.Stack Overflowに投稿して質問して、ついでに質問もブログに貼って、誰かが見たら手伝ってください.もし問題が解決したら、私は問題の原因と解決策をここに置きます.
I meet a problem today. As following.I create a generic Form ,  public class Form1:FormThen I create another inheritance form,  public class From2:Form1.The form 2 cannot be shown in the VS designer,the error message is"all the classes in the file cannot be designed"(this error message is translate from Chinese,the Chinese message isファイル中のクラスは設計できない).But this program can be compiled successfully, and when it runs, both Form1 and From2 can work.
Anyone can give me some help about this ? Thanks.
I am not a native English speaker, I hope I have described my question clear.
----------------------------------------------
Stack Overflowはやはり良いプログラミングサイトで、人気が高く、レベルが高いです.5分後に答えが見つかりました.
以下が答え(テキストアドレス)http://adamhouldsworth.blogspot.com/2010/02/winforms-visual-inheritance-limitations.html)
I have a small personal project going that involves a WinForms GUI.  I have a series of data listing and editing user controls that I decided to refactor.  There were 5 of each, and out of each they used roughly 80%-90% of the same code, just with naming changes.
A fantastic case of "same whitespace, different values" .  I read this a long time ago, but for some reason it stuck.
Supporting Generics
My first port of call was a base class for each of the two user controls.  However, I wanted to migrate even logic involving the business class to the base class, which involved knowing about the business class type.
Step in: Generics.
Step in: first problem .
Using generics causes the designer interface in Visual Studio to crash, as it breaks one of the rules about knowing the base class type.  Microsoft explicitly say generics are not supported.  Fair enough.
The workaround for this (as specified in the previous link) is to create a concrete implementation of the generic base class using a dummy intermediary:
- BaseControl : UserControl- CustomerControl_Design : BaseControl- CustomerControl : CustomerControl_Design
The CustomerControl_Design class will not have any designer support, and you shouldn't actually need it.  But, your CustomerControl class now does - the peasents rejoice.
I've wrapped the code in compiler tags, so that when it doesn't need design-time support, superfluous code is cut:
#if DEBUG
namespace
MyNamespace
{
    using System;
    public partial class CustomerEditorControl_Design : BaseEditorControl<Customer>
   {
       public CustomerEditorControl_Design()      : base()
        {
            InitializeComponent();
        }
    }
}
#endif
public partial class CustomerEditorControl
#if DEBUG  : CustomerEditorControl_Design
#else  : BaseEditorControl<Customer>
#endif
    { ...  }

著作権所有:ベースソフトウェア.作者メールアドレス:[email protected].本文はhttp://www.cnblogs.com/FoundationSoft.文章の転載はこの著作権情報を保持し、出典を明記してください.