防止设计者删除控件成员上的属性

发布于 2024-12-28 12:47:21 字数 647 浏览 2 评论 0原文

我想用属性标记 Windows 窗体上的某些控件。因此,我在 TestAttributes.Designer.cs 中添加了该属性:

[AmbientValue(true)]
private System.Windows.Forms.Label label1;

但是每当我使用设计器的属性窗口更改 label1 的 Modifiers 属性时,设计器都会默默地删除声明中的我的属性:

public System.Windows.Forms.Label label1;

我尝试将声明放入 TestAttributes.cs 中,以免弄乱 .Designer.cs 文件。但是,当更改 Modifiers 属性时,声明将移回 TestAttributes.Designer.cs 并且属性消失。

如何防止设计师删除我的属性?

编辑: 问题应该是:我可以永久将控件的声明移出*.Designer.cs 文件,以便我可以在那里应用属性吗?正如我上面所写,在某些情况下它会被移回。

谢谢你! 丰富

I want to mark certain controls on my Windows Form with Attributes. So I added the Attribute in my TestAttributes.Designer.cs:

[AmbientValue(true)]
private System.Windows.Forms.Label label1;

But whenever I change the Modifiers-property of label1 using the properties-window of the designer, the Designer silently removes my Attribute from the declaration:

public System.Windows.Forms.Label label1;

I tried putting the declaration in the TestAttributes.cs to not mess with the .Designer.cs file. But when changing the Modifiers property the declaration is moved back to TestAttributes.Designer.cs and the Attribute is gone.

How can I prevent the Designer from removing my Attributes?

EDIT:
The question should better be: Can I permanently move the declaration of a control out of the *.Designer.cs file, so I can apply an Attribute there? As I wrote above, it gets moved back in some cases.

Thank you!
richn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

神回复 2025-01-04 12:47:21

如果您想保持设计器支持,我建议使用外部类库。

    namespace ClassLibrary1
    {
        [System.ComponentModel.AmbientValue(true)]
        public class TestClass : System.Windows.Forms.Label
        {
        }
    }

您可以将类似的内容编译到类库中,然后将其导入到 Visual Studio 中。如果您不知道如何操作,请按照以下说明操作。

  1. 右键单击工具箱
  2. 单击“选择项目”
  3. 等待。不要犯现在终止 Visual Studio 的错误。
  4. 选择浏览。
  5. 选择您的 dll 文件。

If you want to keep designer support, I recommend using an external class library.

    namespace ClassLibrary1
    {
        [System.ComponentModel.AmbientValue(true)]
        public class TestClass : System.Windows.Forms.Label
        {
        }
    }

You can compile something like this to a class library, and then import it to visual studio. If you don't know how to do that, follow the instructions below.

  1. Right click the toolbox
  2. Click "Choose Items"
  3. Wait. Do not make the mistake of terminating visual studio now.
  4. Select Browse.
  5. Select your dll file.
风蛊 2025-01-04 12:47:21

我通常不喜欢那些给出“不做”答案但又不做的人。

更改设计师类别是危险的,并且几乎总是会导致意想不到的后果。设计器代码是为编译器保留的,它应该始终看起来像编译器期望的那样。

如果您确实需要设计器代码中的属性,则应该创建一个空应用程序,然后自己编写表单代码。无论如何,如果您不明白如何从头开始修改设计器代码,那么您不应该尝试修改设计器代码

如果这看起来需要大量工作,您始终可以尝试使用设计器作为模型,然后将其复制过来到一个新的非 Windows 窗体项目。

I normally don't like people who give the "don't do it" answer, but don't do it.

Changing the designer class is dangerous, and can almost always lead to unexpected consequences. The designer code is reserved for the compiler, and it should always look like the compiler expects it to look.

If you really wanted attributes in what would otherwise be your designer code, you should instead make an empty application, and do the forms code yourself. You shouldn't be trying to modify designer code anyway, if you don't understand how to do it from scratch

If that seems like a lot of work, you can always try to use the designer as a model, and then copy that over to a new, non-windows forms project.

萌化 2025-01-04 12:47:21

直到最近,我才被引导相信设计师只是代码的一部分,不能被触及。我错了。事实证明,您可以将整个内容撕掉,然后编译器会向您抛出错误。好吧,没关系,只要定义你自己的东西,就完全消除了设计师的必要性。方法如下。假设您有一个标签

<div id="div1" runat="server">

,并且希望在没有任何 Designer.cs 文件存在的情况下使其在背面不可见(我们已将其删除)。首先将其与其他声明一起引用(即在 page_load 之外,外部的某个地方)。

protected System.Web.UI.HtmlControls.HtmlGenericControl div1;

但是请记住,您必须引用在具有 runat="server" 标记的前端使用的所有内容。假设您有一个仅包含 runat="server" 标签的页面,在背面我们将其引用为

namespace yournamespace.something
{
    public class yourpagetitle: System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Image imgLogo;
        protected System.Web.UI.WebControls.TextBox tbDate;
        protected System.Web.UI.WebControls.Label yourLabelName;

注意我添加了图像和文本框作为附加示例。还有哈扎!因此,您完全不再需要 Designer.cs 文件。哦,请注意,designer.cs 文件只不过是我们上面所做的,但它会自动为您完成它。 ...大多数时候...当失败时,是时候以自己的方式处理事情了。我确信如果你重新格式化你的计算机,重新安装所有东西等等,毫无疑问,一切都会再次工作。对于那些没有时间照顾和解决微软遇到的每一个小问题的人来说,这只是一个合法的解决方法。

Untill recently i was lead to believe that designer was just part of the code and it was not to be touched. I was wrong. Turns out you can rip the entire thing out and then compiler will throw an error at you. Okay, that's fine, just define your own stuff, completely thus eliminating the necessity for a designer. here is how. Say you have a tag

<div id="div1" runat="server">

and you want to make it invisible on the back side without any designer.cs file present (we deleted it). First reference it along with other declarations (i.e. outside of page_load, somewhere on the outside)

protected System.Web.UI.HtmlControls.HtmlGenericControl div1;

Remember however, you MUST reference EVERYTHING that you use on the front side that has a runat="server" tag. So say you have a page with only a runat="server" label, on the back we reference to it as

namespace yournamespace.something
{
    public class yourpagetitle: System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Image imgLogo;
        protected System.Web.UI.WebControls.TextBox tbDate;
        protected System.Web.UI.WebControls.Label yourLabelName;

Notice I added image and a textbox as additional examples. And huzza! You have thus completely eliminated the need for designer.cs file. Oh, and do notice, designer.cs file is nothing more than what we just did above, but it does it for you automatically. ...most of the time... When it fails, time to handle things your own way. I'm sure if you reformat your computer, reinstall everything, etc etc etc things will work again, no doubt. This is just a legitimate work around for those who do not have time to babysit and troubleshoot every little hick-up Microsoft does.

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