在 C# 中组织 gui 代码
在具有许多不同组件的单一表单应用程序中管理代码的最佳方法是什么? 例如,考虑一个具有产品选择器的金融应用程序,用于浏览/选择要查看的产品; 一些价格、利率或其他内容的实时行情; 滚动新闻源; 各种图表; 显示本地计算值的网格; 等等,
您是否会为每个不同的组件创建一个自定义控件,即使它们不会在此应用程序之外使用? 或者你可以用实现每个组件逻辑的类来做到这一点,并以某种方式更新 GUI 中的实际控制吗? 两个组件可能需要彼此交互,例如,您单击网格中的一个单元格,它会显示一些图表(主表单会处理发送消息吗?)
我有一个习惯,随着功能的添加,让表单代码变得臃肿我真的很想熟悉一种更好的方法。 我正在使用 c#(不使用 WPF),但我想基本设计原则不一定是特定于语言的。
what are the best ways to manage code in a single-form app that has many different components? for example, think of a financial app that has a product picker for browsing/choosing a product to view; a handful of real-time tickers for prices, interest rates, or whatever; a scrolling news feed; various charts; a grid displaying locally calculated values; etc.
would you create a custom control for each distinct component, even if they're not going to be used outside of this app? or could you do it with classes that implement the logic for each component and somehow update that actual control in the gui? two components may need to interact with each other, e.g. you click a cell in a grid, and it brings up some chart (would the main form handle sending the message?)
I have a habit of letting form code get bloated as features are added and I really want to get familiar with a better way. I'm working with c# (not using WPF) but I guess basic design principles aren't necessarily language-specific.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试MVP 模式。
You can try the MVP pattern.
请参阅 Martin Fowler 的 Gui 架构
See - Gui Architectures by Martin Fowler
这在一定程度上取决于应用程序的规模有多大,以及应用程序的生命周期。
在几乎任何大小合理的应用程序中,我建议将各个部分分成单独的 UserComponents。 话虽这么说,有很多方法可以超越这一点,包括使用插件/DI 等。
我建议阅读(或至少浏览)复合客户端应用指南 获取想法。 详细讨论了消息传递问题以及将各个组件连接在一起的不同方法的问题。
复合客户端应用指南中有许多相关的要点,即使您没有使用 WPF 或 Silverlight。 指南中的许多部分都不是特定于技术的 - 它们更多地涉及如何将多个部分组合在一起、促进设计的可重用性和灵活性等。无论您使用什么技术,这些内容都适用。
It somewhat depends on how large the application scale is, and also the lifetime of the application.
In nearly any reasonably sized application, I'd recommend separating individual sections into separate UserComponents. That being said, there are many ways to move beyond that, including using plugins/DI, etc.
I'd recommend reading (or at least skimming) the Composite Client Application Guidance for ideas. The message passing questions as well as the questions on different approaches to tie together individual components are discussed in detail.
There are many gems that would be relevant in the Composite Client Appilcation Guidance, even though you're not using WPF or Silverlight. Many of the sections in the guidance are not technology specific - they relate more to how to bring together multiple pieces, promote reusability and flexibility in the design, etc. These apply no matter what technology you are using.
我倾向于将所有事情都作为自定义控件来完成,并且他们使用面板来放置我的位置。 然后,我创建该控件的一个新实例并将其添加到面板中。
这允许我执行自定义构造函数,当我尝试在当前项目中使用 DI/IOC 框架时,我发现它更有用。
I tend to do everything as a custom control and them use panels for my placement. I then create a new instance of the control and add it to the panel.
This allows for me to do custom constructors, which I have been finding more useful as I am trying to use a DI/IOC framework on my current project.