Winforms自定义属性无法重新加载 - 已编辑 [英] Winforms custom property fails to reload - edited

查看:66
本文介绍了Winforms自定义属性无法重新加载 - 已编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Component对象中有一个自定义属性,它是一个包含另一个对象的对象,该对象是另一个对象的泛型集合,也是一个对象。它看起来像这样:

I have a custom property in my Component object that is an object that contains another object that is a generic collection of another object, as well as a single object. It looks something like this:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Editor(typeof(ControlsEditor), typeof(UITypeEditor))]
public DialogControls Controls { get; set; } = new DialogControls();

[Serializable]
public class DialogControls :
    ControlGroup<Control> Controls { get; set; } = new ControlGroup<Control>();
    Control SelectedItem { get; set; } = null;
}

[Serializable]
public class ControlGroup<T> : IList<T>, ICollection where T:Control{
    List<T> _controls;
    // Implement interfaces here
}

[Serializable]
public abstract class Control {
   //... a couple of protected members and abstract methods
}



从Control继承的所有类都是可序列化的。



当我进入通过自定义编辑器的控件列表,一切正常,并且属性按预期保存在资源文件中。但是,当我为包含组件的表单加载设计器时,我收到错误

类型为'System.Collections.Generic.List`1的对象[CommonItemDialog.CommonFileDialog + Control] '无法转换为'System.Collections.Generic.List`1 [CommonItemDialog.CommonFileDialog + Control]'。第264行,第5位无法解析。



如果我关闭Visual Studio,然后重新打开解决方案,表单就会很好,所有我的属性设置完好无损。如果我修改另一个模块(即使是注释),那个错误会在设计器中返回,我必须关闭VS并重新打开它以摆脱它。





我发现只有在添加了复杂的Control对象时才会出现这种情况 - 以下代码演示:


All of the classes that inherit from Control are also serializable.

When I enter a list of controls through the custom editor, everything works fine, and the property is saved in the resources file as is expected. However, when I load the designer for the form that contains the component, I get the error
Object of type 'System.Collections.Generic.List`1[CommonItemDialog.CommonFileDialog+Control]' cannot be converted to type 'System.Collections.Generic.List`1[CommonItemDialog.CommonFileDialog+Control]'. Line 264, position 5. cannot be parsed.

If I close Visual Studio, and then reopen the solution, the form comes up fine, and all my property settings are intact. If I modify another module, however (even a comment), that error returns in the designer, and I have to close VS and reopen it to get rid of it.


I have found that this only occurs when I have a complex Control object added - following code demonstrates:

public class ControlItem : Control {
   public string Label { get; set; }
   // All virtual methods implemented
}

public interface IControlContainer<T> where T:Control {
   ControlGroup<ControlItem> Controls { get; set; }
}

public class ComboBox : IControlContainer<ControlItem>, Control {
   public ControlGroup<ControlItem> Controls { get; set; } = new ControlGroup<ControlItem>();
}



当我添加像ComboBox这样的控件时,问题就出现了。



仅供参考 - 这些控件成为IFileDialogCustomize实现中的自定义控件。当我运行测试表单时,添加了这些控件,它可以正常工作,并且ComboBox会在CommonItemDialog上正确显示。





当列表中的对象是列表的泛型类型的衍生物时,问题似乎与反序列化通用列表的元素直接相关(即,控件列表遇到了反序列化ComboBox对象的问题)。

我正在尝试的是三重:


Its when I add a control like the ComboBox that my problems start.

Just a FYI - these controls become the custom controls in a IFileDialogCustomize implementation. When I run the test form, with these controls added, it works perfectly, and the ComboBox DOES appear correctly on the CommonItemDialog.


The problem seems to be directly related to deserialising the elements of the generic lists, when the object in the list is a derivative of the generic type of the list (i.e. The list of Controls hits a problem deserialising a ComboBox object).
What I am trying is threefold:

  1. 从外部集合类中删除泛型参数,并为每个集合声明不同的类型。
  2. 在所有Control对象上实现ISerializable。这还涉及确保每种类型都有一个默认构造函数。
  3. 将通用列表序列化和反序列化为数组而不是List< T> s



希望其中一个改变可以解决问题 - 如果他们这样做,我会将其作为答案发布。



我尝试过:



我已经尝试将DesignerSerializationVisibility重置为Content,我得到一个类似的错误,我以同样的方式解析(即关闭VS并重新打开解决方案) )。


Hopefully one of these changes will solve the problem - I'll post it as an answer if they do.

What I have tried:

I've tried resetting the DesignerSerializationVisibility to Content, and I get a similar error that I resolves itself in the same way (i.e. close VS and reopen solution).

推荐答案

这听起来像我几年前遇到的东西,VS决定一个类与它自己不一样。看看这里:在决定使用Visual Studio时一个类与它自己不一样...... [ ^ ]并清理中间文件 - 这可能会解决问题。
It sounds like something I met a few years back, where VS decides that a class isn't the same as itself. Have a look here: Getting round Visual Studio when it decides a class isn't the same as itself...[^] and clean out the intermediate files - that may fix the problem.


这篇关于Winforms自定义属性无法重新加载 - 已编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆