C#Winforms Visual Studio设计器找不到自定义类型

时间:2011-01-13 08:48:20

标签: c# visual-studio winforms visual-studio-2010

我有一个自定义控件,其中包含自定义类型的通用列表。此列表是公开定义的:

[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

当我使用字符串而不是自定义对象时,desginer会保存我的列表。 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);
    }
}

1 个答案:

答案 0 :(得分:2)

您可以尝试将它们序列化为后面的代码,而不是将按钮序列化为资源文件。为此,您需要为TypeDescriptor类型实施自定义CompactButton,然后处理转换为InstanceDescriptor。看看How to Implement a TypeConverter。例如:

[TypeConverter(typeof(CompactButtonTypeConverter))]
public class CompactButton: ... {
  ...
}

public class CompactButtonTypeConverter: TypeConverter {

  public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
    if (destinationType == typeof(InstanceDescriptor)) 
       return true;
    return base.CanConvertTo(context, destinationType);
  }

  public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
    if (destinationType == typeof(InstanceDescriptor) && value is CompactButton) {
      // This assumes you have a public default constructor on your type.
      ConstructorInfo ctor = typeof(CompactButton).GetConstructor();
      if (ctor != null) 
         return new InstanceDescriptor(ctor, new object[0], false);
    }
    return base.ConvertTo(context, culture, value, destinationType);      
  }

}

有关详细信息,请参阅InstanceDescriptor课程。

更新:对于您的UITypeEditor和CompactButtons属性,您不需要设置器。更改CompactButtons属性,如下所示:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(typeof(ButtonPanelXEditor), typeof(UITypeEditor))]
public List<CompactButton> CompactButtons
{
    get { return _compactButtons; } // _compactButtons must of course be initialized.
}

然后你可以实现UITypeEditor的EditValue方法,如下所示:

public override object EditValue(System.ComponentModel.ITypeDescriptorContext context,
  IServiceProvider provider, object value) {
  if (context == null || provider == null)
    return null;

  var b = context.Instance as ButtonPanel;
  if (b == null)
    return value;

  var editorService = (IWindowsFormsEditorService)
    provider.GetService(typeof(IWindowsFormsEditorService));
  if (editorService == null)
    return null;

  // This constructor should copy the buttons in its own list.
  using (var form = new FooBar(b.CompactButtons)) {
    if (editorService.ShowDialog(form) == DialogResult.OK && context.OnComponentChanging()) {
      b.CompactButtons.Clear();
      b.CompactButtons.AddRange(form.Buttons);
      context.OnComponentChanged();
    }
  }
  return value;
}

如果您的编辑表单不是非常专业,您可以尝试CollectionEditor