如何在 Designer.cs 中强制序列化新属性
我向组件添加了一个新属性,用于唯一标识项目中的每个网格控件,称为 GridIdentifier:
public class MyCustomGridControl : GridControl
{
private string gridIdentifier = "empty";
[Browsable(true)]
[DefaultValue("empty")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string GridIdentifier
{
get { return gridIdentifier; }
set { gridIdentifier = value; }
}
public MyCustomGridControl()
{
if (this.gridIdentifier == "empty")
this.gridIdentifier = Guid.NewGuid().ToString();
}
}
问题是,对于表单中的现有控件,表单仅在我更改表单中的某些内容(读取:任何内容)后序列化新属性。它可能是表单的标题、大小等。
但我希望看到的是,当我打开表单时,它检测到表单已更改,因此我可以保存它,并且新属性会被序列化。
有谁知道为什么打开表格后新属性没有保存以及如何修复它?当然,任何其他有帮助的想法也将受到赞赏。
I added a new property to a component to uniquely identify every gridcontrol in my project, called GridIdentifier:
public class MyCustomGridControl : GridControl
{
private string gridIdentifier = "empty";
[Browsable(true)]
[DefaultValue("empty")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string GridIdentifier
{
get { return gridIdentifier; }
set { gridIdentifier = value; }
}
public MyCustomGridControl()
{
if (this.gridIdentifier == "empty")
this.gridIdentifier = Guid.NewGuid().ToString();
}
}
The problem is that for existing controls in my forms, the form only serializes the new property after I change something (read: anything) within the form. It might be the caption of the form, the size, etc.
But what I would like to see is that it detects that the form has changed when I open it, so I can save it and the new property gets serialized.
Does anyone have a clue why the new property doesn't get saved after opening the form and how to fix it? Any other ideas that help are of course also appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜它正在做基本的健全性检查(即应该有什么改变) - 以防止意外的源代码更改(我讨厌打开文件时可能会导致副作用- 我在看着你,DBML!)。
顺便说一句,强制序列化通常(我认为由于上述原因它不会适用):
bool ShouldSerialize*()
和void Reset *()
是框架使用的约定。I would guess it is doing basic sanity checking (i.e. should anything have changed) - to prevent unexpected source code changes (I hate it when opening a file can cause side-effects - I'm looking at you, DBML!).
On a side note, to force serialization generally (I don't think it will apply due to the above):
the
bool ShouldSerialize*()
andvoid Reset*()
are a convention used by the framework.