繁体   English   中英

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

[英]C# Winforms Visual Studio designer cannot find custom type

我有一个带有通用类型的自定义类型的自定义控件。 此列表是公共定义的:

[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类型实现自定义TypeDescriptor ,并在那里处理到InstanceDescriptor转换。 看一下如何实现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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM