【发布时间】:2011-01-13 08:48:19
【问题描述】:
我有一个带有自定义类型通用列表的自定义控件。此列表定义为公开:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
public List<CompactButton> CompactButtons
{
get { return _compactButtons; }
set { _compactButtons = value; }
}
当我将此控件添加到我的表单并构建我的项目时,我收到此错误:
错误 1 找不到名称的类型。类型名称为 'ButtonPanelX.CompactButton, ButtonPanelX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'。第 127 行,位置 5。D:\Projecten\ButtonPanelX\ButtonPanelX\Form1.resx 127 5 ButtonPanelX
当我使用字符串而不是自定义对象时,设计器会保存我的列表。 CompactButton 具有属性[Serializable] 并派生自ISerializable
我能做些什么来解决这个问题?
编辑:
public class ButtonPanelXEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
// We will use a window for property editing.
return UITypeEditorEditStyle.Modal;
return base.GetEditStyle(context);
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
context.OnComponentChanging();
ButtonPanel b = context.Instance as ButtonPanel;
FooBar form = new FooBar();
form.Buttons = b.CompactButtons;
form.ShowDialog();
b.CompactButtons = form.Buttons;
b.DrawButtons();
context.OnComponentChanged();
return form.Buttons;
}
}
编辑 2:
[Serializable]
public partial class ButtonPanel : UserControl
{
private ArrayList _compactButtons;
public ButtonPanel()
{
InitializeComponent();
_compactButtons = new ArrayList();
AddButtons();
this.Load += new EventHandler(ButtonPanel_Load);
}
void ButtonPanel_Load(object sender, EventArgs e)
{
DrawButtons();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
public ArrayList CompactButtons
{
get { return _compactButtons; }
}
public void DrawButtons()
{
baseButton1.Visible = ((CompactButton)_compactButtons[0]).Visible;
baseButton2.Visible = ((CompactButton)_compactButtons[1]).Visible;
}
private void AddButtons()
{
/* Buttons baseButton1 and baseButton2 are created by the designer */
CompactButton c = new CompactButton();
c.Enabled = baseButton1.Enabled;
c.Visible = baseButton1.Visible;
c.Name = baseButton1.Name;
CompactButton c2 = new CompactButton();
c2.Enabled = baseButton2.Enabled;
c2.Visible = baseButton2.Visible;
c2.Name = baseButton2.Name;
_compactButtons.Add(c);
_compactButtons.Add(c2);
}
}
【问题讨论】:
-
你的类型是 CompactButton 还是 CompactButtons?在您的代码 sn-p 中,您将其称为 CompactButton,而在问题文本中您说 CompactButtons。
-
我的类型名为
CompactButton。我已经编辑了我的问题。 -
您项目的目标框架版本是什么?谷歌表示如果目标是 3.5 框架会有一些问题。
-
我也发现了,但是将其更改为版本 4 并不能解决我的问题。
-
为什么要将按钮序列化到资源文件而不是后面的代码?
标签: c# visual-studio winforms visual-studio-2010