Visual C# GUI 设计器 - 删除生成的事件处理程序代码的推荐方法

发布于 2024-08-27 04:11:06 字数 959 浏览 4 评论 0原文

我是 Visual C# 设计器的新手,因此这些是关于如何与设计器一起工作的一般且非常基本的问题。

例如,当我们向表单添加标签,然后在 Visual C# 设计器(我使用的是 Microsoft Visual C# 2008 Express Edition)中双击它时,会发生以下情况:

  1. 设计器在 Form1 中生成代码。 Designer.cs(为简单起见,采用默认名称)添加标签,
  2. 然后双击它会将事件处理程序 label1_Click 添加到 Form1.Designer.cs 中的标签>,使用以下代码

    this.label1.Click += new System.EventHandler(this.label1_Click);

它将事件处理程序方法添加到 Form1.cs

private void label1_Click(object sender, EventArgs e)
{

}

如果我现在删除标签,则只会删除 Form1.Designer.cs 中的代码,但 label1_Click 方法将保留在 Form1.cs 中,即使它不是没有被其他任何东西使用。但是,如果我在设计器内的 Click 事件的 Properties->Events 中使用重置,则 Form1.cs 中的 label1_Click 方法将被删除。

1.) 这不是有点不一致的行为吗?

2.) 删除此类生成的事件处理程序代码的推荐方法是什么?

3.) 使用设计器的最佳“思维方法”/最佳实践是什么?

我会通过精神分离的方式来处理它,因为 Form1.cs 是我 100% 的责任,而另一方面我根本不接触 Form1.Designer.cs 中的代码。这有道理吗?因为有时设计师会删除某些东西。来自 Form1.cs 我对此不确定。

I'm new to the Visual C# designer so these are general and pretty basic question on how to work with the designer.

When we for instance add a label to a form and then double-click on it in the Visual C# designer (I'm using Microsoft Visual C# 2008 Express Edition), the following things happen:

  1. The designer generates code within Form1.Designer.cs (assume default names for simplicity) to add the label,
  2. then with the double-click it will add the event handler label1_Click to the label within Form1.Designer.cs, using the following code

    this.label1.Click += new System.EventHandler(this.label1_Click);

and it adds the event handler method to Form1.cs

private void label1_Click(object sender, EventArgs e)
{

}

If I now remove the label only the code within Form1.Designer.cs will be removed but the label1_Click method will stay within Form1.cs even if it isn't used by anything else. But if I'm using reset within Properties->Events for the Click-event from within the designer even the label1_Click method in Form1.cs will be removed.

1.) Isn't that a little inconsistent behavior?

2.) What is the recommended way of removing such generated event handler-code?

3.) What is the best "mental approach"/best practice for using the designer?

I would approach it by mental separation in the way that Form1.cs is 100% my responsibility and that on the other hand I'm not touching the code in Form1.Designer.cs at all. Does that make sense or not? Since sometimes the designer removes sth. from Form1.cs I'm not sure about this.

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

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

发布评论

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

评论(2

机场等船 2024-09-03 04:11:06

1)是的,这是不一致的。一点。
2)我使用了更简单的方法:简单地清除所有处理代码并尝试编译=>编译器将告诉您在哪里删除事件分配。尽管看起来很吓人,但它确实很安全。
3)以下是我推荐并在我的软件部门强制执行的最佳实践:
3a)切换到WPF(单独询问WPF最佳实践;还有很多其他问题);
3b) 绝不允许 Visual Studio 自动生成事件代码(WPF 或 Windows.Forms);如果发生意外使用 (2) 尽快;
3b) 对于事件分配,使用匿名 lambda:

ByButton.Click += (source, evArg) => { SomeHandler(...); };

对于 v.2.0:

ByButton.Click += delegate(object source, EventArgs evArgs) { SomeHandler(...); };

有很多好处:您的处理程序不必使用特定的方法配置文件;你可以将整个代码放入匿名处理程序中,只要它足够短,以 lambda 形式,你可能永远不需要知道事件参数的类型...

1) Yes, it is inconsistent. A little.
2) I used MUCH MORE SIMPLE approach: simple wipe out all your handle code and try to compile => compiler will show you where to wipe out an event assignment. Despite of scary look, it is really safe.
3) Here is my best practices which I recommend and kind of enforce in my software department:
3a) Switch to WPF (ask for best WPF practices separately; there are a lot of other problems);
3b) NEVER ever allow Visual Studio to auto-generate event code (WPF or Windows.Forms); in case of accident use (2) as soon as possible;
3b) For event assignment use anonymous lambda:

ByButton.Click += (source, evArg) => { SomeHandler(...); };

for v.2.0:

ByButton.Click += delegate(object source, EventArgs evArgs) { SomeHandler(...); };

There are many benefits: your handlers are not bound to using specific method profile; you can put whole code inside anonymous handler is it is short enough, in lambda form you may never need to know the type of Event Arguments...

你是年少的欢喜 2024-09-03 04:11:06

Visual Studio 中可能内置了安全元素。

例如:

  • 添加按钮A和点击事件。
  • 从另一个按钮 B 引用按钮 A 单击事件。
  • 删除按钮A
    • 如果代码运行,则按钮 B 将会损坏
    • 如果代码保留,则按钮 B 继续工作。

我通常会注释掉 Designer.cs 文件中中断的任何代码(事件处理程序)。

There probably is a element of safety built into Visual Studio.

For example:

  • Add a button A and a click event.
  • Reference the button A click event from another button B.
  • Remove button A
    • if the code were to go then button B would break
    • if the code remains then button B continues to work.

I generally comment out any code (event handlers) that break in the designer.cs file.

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