且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不能添加控制形成

更新时间:2023-12-06 14:52:58

阅读DesignerSerializationVisibility枚举

把这个属性在MessageList中的财产

  [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
 

在这种情况下,设计师会失去用户的变化将MessageList 但如果你将其设置为

  [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
 

设计师将保存属性的内容,控制主机的Designer.cs

I'm trying to add a Usercontrol to a form. The UserControl is in a separate project than the form, but is in the same solution. I have added this control to other forms in the past, however, something has changed and I get the following error:

"Failed to create the component MessageDisplayListControl. The error message follows: 'System.Runtime.Serialization.SerializationException: Type AceXtremeNET.Utilities.Message' in Assymbly AceXtremeNET, Version=10.0.0.273,...... is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMember(RuntimeType type) at ...."

The control does get added to the .Designer.cs, however, it is not displayed in the visual GUI. Everytime I try to build, I get multiple errors that give the same basic error as above, that the 'AceXtremeNET.Utilities.Message' is not Serializable.

--------- Edit ------------------

My control has the following property which appears to the problem.

public IList<Message> MessageList {get{return _getList();} {set {_lostList(value);}}

No code in the control is dependant on this property as it was meant purley as a get/set accessor. Whenever I comment out the code, Everything appears to work correctly. Otherwise I can the error I mentioned above. I have receieved another error on build as I mentioned before, and it appears this is the only Property that is trying to be Serialized.

--------- Edit (Stack Trace)------------------

at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialzation.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, SerializationBinder binder)
at System.Runtime.SerializationFormatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)
...

--------- Edit (Control Properties)------------------

public MessageControl MessageDisplay {get{return messageControl1;}} // This is another user control I created.  I've not had any problems with this control.
public MessageListBox {get { return listBox1; } }
public int MessageCount { get { return MessageListBox.Items.Count; }}
public bool ValidSelection { get { return (SelectedIndex >= 0 && SelectedIndex < MessageCount); } }
public Message SelectedMessage { get { return listBox1.SelectedItem as Message; } set { MessageDisplay.Message = Value; } }
public int SelectedIndex { get { return listBox1.SelectedIndex; } set { listBox1.SelectedIndex = value; } }

read about DesignerSerializationVisibility Enumeration

put this attribute  on MessageList property

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

in this situation designer will lost user change in MessageList but if you set it to

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

Designer will save the Property Content in Designer.cs of control host