2011-01-12 65 views
2

存储设计时间值在项目中,我有一个自定义面板:C#不能在设计师的Visual Studio 2010

[Serializable] 
public partial class ButtonPanel : UserControl 
{ 
    private List<CompactButton> _compactButtons; 

    public ButtonPanel() 
    { 
     InitializeComponent(); 

     _compactButtons = new List<CompactButton>(); 

     AddButtons(); 
    } 

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))] 
    public List<CompactButton> CompactButtons 
    { 
     get { return _compactButtons; } 
     set { _compactButtons = value; } 
    } 

    private void AddButtons() 
    { 
     CompactButton c = new CompactButton(); 
     c.Enabled = baseButton1.Enabled; 

     CompactButton c2 = new CompactButton(); 
     c2.Enabled = baseButton2.Enabled; 

     _compactButtons.Add(c); 
     _compactButtons.Add(c2); 
    } 
} 

在面板上有两个按钮(baseButton1和baseButton2)。这些按钮的状态必须保存在设计器中。但由于Button类是不可序列化,我创建了一个自定义的类来存储最重要的属性,CompactButton

[Serializable] 
public class CompactButton 
{ 
    #region Member variables 
    private bool _visible; 
    private bool _enabled; 
    private string _tooltip; 
    private string _name; 
    #endregion 

    #region Constructor 
    public CompactButton() 
    { } 
    #endregion 

    #region Properties 
    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 
    public bool Visible 
    { 
     get { return _visible; } 
     set { _visible = value; } 
    } 
    public bool Enabled 
    { 
     get { return _enabled; } 
     set { _enabled = value; } 
    } 
    public string ToolTipText 
    { 
     get { return _tooltip; } 
     set { _tooltip = value; } 
    } 
    #endregion 
} 

正如你可以在ButtonPanel类看,我有一个属性CompactButtons它返回一个列表与CompactButtons

当我加入我的ButtonPanel在设计,然后建立我的应用程序,我得到这个错误:

Error 1 Could not find a type for a name. The type name was 

'System.Collections.Generic.List`1[[ButtonPanelX.CompactButton, ButtonPanelX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Line 131, position 5. D:\Projecten\ButtonPanelX\ButtonPanelX\Form1.resx 131 5

我有什么做的,解决这一问题?

+0

+1:有趣的问题。我的第一个想法是,* CompactButtons不可序列化*。但是,你已经涵盖了这一点。我的第二个想法是可能从`ISerializable`派生CompactButtons。 – IAbstract 2011-01-12 20:02:59

回答

0

我做了一个新项目,粘贴了你的例子,它只是适用于我(删除了对样本中未包括的东西的引用)。

从你的错误信息,我想你改变了代码中的东西,并重新编译,现在RESX序列化的格式不再有效。请注意,一旦获得序列化数据,您在序列化的类中的what you can change的范围就非常有限。

我建议从设计器文件手动删除控件(使用xml编辑器打开resx)并再次放置该控件。

我有一个额外的提示:你根本不需要在CompactButton上使用[Serializable]。如果删除它,VisualStudio将通过代码将序列化到designer.cs文件而不是resx文件中。我几乎总是喜欢这个,因为那时我可以手动编辑生成的代码。