无法将控件添加到表单

Cannot Add control to form(无法将控件添加到表单)
本文介绍了无法将控件添加到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于无法将控件添加到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)