调用事件的正确位置?

发布于 2024-07-06 11:00:54 字数 281 浏览 5 评论 0原文

我有一个刚刚启动的 Windows 窗体项目。 在表单上,​​我有一个正在加载产品的列表框。 当有人双击产品时,我希望它引发 ProductChanged 事件。 当产品更改时,我的项目中的其他内容将订阅此事件并更新 GUI 的其他部分等内容。

我的问题是,该事件应该在哪里以及谁应该提出? 该事件应该在表单上并由列表框的双击处理程序引发吗? 或者我应该将其设置在产品类中的事件位置,并且列表框调用此类上的方法以使其知道产品已更改。 然后 Product 类会引发该事件? 或者应该以另一种方式完成?

I have a Windows Form project that I've just started. On the form I have a listbox that I'm loading with Products. When someone double clicks a Product, I want it to raise a ProductChanged event. Other things in my project will subscribe to this event and update things like other parts of the GUI when the Product changes.

My question is, where should the event be and who should raise it? Should the event be on the form and be raised by the listbox's double click handler? Or should I set it up where the event is in my Products class and the listbox calls a method on this class to let it know that the product has changed. And then the Product class will raise the event? Or should it be done another way?

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

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

发布评论

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

评论(4

财迷小姐 2024-07-13 11:00:54

产品更改是一个 UI 事件,如果它在表单上引发,并且其他 UI 元素订阅该事件,则有意义。

The product changed is a UI event, it would make sense if its raised on the form, and the other UI elements suscribe to the event.

淑女气质 2024-07-13 11:00:54

列表框应该引发该事件,因为其产品已更改。

The listbox should be raising the event since that's whose Product has changed.

两相知 2024-07-13 11:00:54

取决于您的事件意味着什么。 如果目的是通知订阅者 Product 对象已修改,那么它属于 Product 类,并且应该由该类内的代码触发。 如果目的是通知选择了另一个 Product 对象,则它属于管理产品列表(您的列表框/表单)的视图。

Depends on what your event signifies. If the intent is to notify subscribers that the Product object was modified, then it belongs to the Product class and should be fired by a code inside that class. If the intent is to notify that another Product object was selected, then it belongs to the view that manages the list of Products (your listbox/form).

寂寞美少年 2024-07-13 11:00:54

名称 ProductChanged 意味着该事件在产品更改时触发。 我会在产品类中引发该事件,因为在这种情况下,每个更改都会触发该事件。 如果您在 GUI 中触发它,然后添加(例如)导入功能,则很可能您忘记在更改后触发该事件。

The name ProductChanged implies that the event is fired while the product has been changed. I would raise the event in the product class, because in that case each change will trigger the event. If you trigger it in the GUI, and later add (for example) import functionality, chances are that you forgot to fire the event after the change.

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