【发布时间】:2017-06-07 14:17:09
【问题描述】:
我想了解如何分离我的 GUI 和工作代码。 下面的人为示例代码是最小的起点, 我能想到这涵盖了这个想法。
该示例使用 Windows 窗体作为命令源和显示 (消费者)的结果。我希望工作代码能够从命令行界面获取命令。工作代码不应依赖于对表格的了解。表单应该对工作代码知之甚少。当工作代码中的属性更改值时,我希望有几个消费者“看到”。
我猜这意味着使用事件进行通信,也许还有接口,但我对任何事情都持开放态度。
那里有一百万种不同的建议。我已经阅读了设计模式书籍,并且尝试了很多,但还没有找到一个解释得很好的集合,我可以完全实现它。
我不想要一个通用的解决方案。我想要一个尽可能简单的 为小型个人项目实施和维护。我不是 为大型公司设计。
我发现的大多数解决方案都会提示该怎么做,但不会涵盖 诸如声明事件的位置以及其他事件的具体情况 一段代码找出事件的存在,因此它可以发出 事件或响应事件。我总是在某个地方最终需要一个相当于全局变量的东西来将事物连接在一起。
在这里,我能找到的与我的问题最接近的匹配是:C# Windows Forms App: Separate GUI from Business Logic 但该解决方案使用表单来创建工作者的实例并直接返回一个值,而不是通知任何感兴趣的观察者。提供的解决方案将这两个类紧密绑定。
我在任何地方找到的最接近的解决方案是:https://www.codeproject.com/Articles/14660/WinForms-Model-View-Presenter 在接口和反射方面确实有一些真的很酷,但似乎不太可维护也不太灵活。
下面源代码中的注释行显示了所需的交互 点,但没有实现。
文件#1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// tell an instance of JustCounts to increment by 10
}
// Here, allow JustCounts to cause a call to this (or something
// similar, perhaps a property) to inform this code that the TotalCount
// property has changed.
public void ShowNewTotalCount(int NewTotal)
{
Console.WriteLine("New Total Count = {0}", NewTotal);
}
}
文件 #2
class JustCounts
{
private int m_TotalCount = 100;
// Inform other classes when the following property changes value,
// preferably including the value in the notification.
public int TotalCount { get => m_TotalCount; }
// The code in File #1 needs to result in a call to this method
// along with the correct argument value.
public void AddThisMuch(int increment)
{
m_TotalCount += increment;
}
}
【问题讨论】:
标签: c# user-interface events