存储设计时间值在项目中,我有一个自定义面板: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
我有什么做的,解决这一问题?
+1:有趣的问题。我的第一个想法是,* CompactButtons不可序列化*。但是,你已经涵盖了这一点。我的第二个想法是可能从`ISerializable`派生CompactButtons。 – IAbstract 2011-01-12 20:02:59