如何在 Designer.cs 中强制序列化新属性

发布于 2024-11-06 02:52:07 字数 764 浏览 2 评论 0原文

我向组件添加了一个新属性,用于唯一标识项目中的每个网格控件,称为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愚人国度 2024-11-13 02:52:07

我猜它正在做基本的健全性检查(即应该有什么改变) - 以防止意外的源代码更改(我讨厌打开文件时可能会导致副作用- 我在看着你,DBML!)。

顺便说一句,强制序列化通常(我认为由于上述原因它不会适用):

public bool ShouldSerializeGridIdentifier() { return true; }

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):

public bool ShouldSerializeGridIdentifier() { return true; }

the bool ShouldSerialize*() and void Reset*() are a convention used by the framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文