简体   繁体   English

视窗之间的互动

[英]Interactions between windows

what's the best/proper way of interacting between several windows in C# app? 在C#应用程序中的多个窗口之间进行交互的最佳/正确方式是什么? Recently, I've run into a problem where one of program windows has to call method modifying main window. 最近,我遇到了一个问题,其中程序窗口之一必须调用修改主窗口的方法。 My solution was to create factory-like class, that would arrange all underlying model-data and organize the communication between various windows (through delegates). 我的解决方案是创建类工厂类,该类将安排所有基础模型数据并组织各个窗口之间的通信(通过委托)。 However, as passing one or two delegates was not a problem, I started thinking what if my other windows would need 10 delegates to interact properly with main window? 但是,由于通过一个或两个代表不是问题,所以我开始考虑如果我的其他窗口需要10个代表才能与主窗口正确交互该怎么办? Are delegates good solution? 代表是好的解决方案吗? How to pass them in good way - through constructor, properties? 如何以良好的方式传递它们-通过构造函数,属性? Or maybe the need of using that many delegates is some serious design flaw itself? 还是可能需要使用这么多的代表本身就是一些严重的设计缺陷?

You need to split the Model from the view by a Controller. 您需要通过控制器从视图中拆分模型。 Put an object that will be able to have both form reference and that will be able to manage the shared information. 放置一个既可以具有表单引用又可以管理共享信息的对象。

An other solution is to send the reference of the formX to the formY this way they can communicate. 另一种解决方案是将formX的引用发送到formY,以便他们可以进行通信。

We use a custom built forms manager that uses the subject/observer pattern. 我们使用使用主题/观察者模式的定制构建的表单管理器。

Every form that is opended is reported to the FormsManager, the FromsManager makes itself an observer of the form. 将打开的每个表单都报告给FormsManager,FromsManager使自己成为表单的观察者。

It also makes the form an observer of the FormsManager. 它还使表单成为FormsManager的观察者。

So you end up with every form observing the FormsManager and the FormsManager observing every form. 因此,您最终要遵守FormsManager的每个表单,而遵守每种表单的FormsManager都要结束。 Each form can then communicate to any other form via the FormsManager without each form having to be aware of all the others. 然后,每个表单都可以通过FormsManager与任何其他表单进行通信,而每个表单不必知道所有其他表单。

If it's only needing to interact with the main window, why not give a reference to the main window in the constructor of the others? 如果只需要与主窗口进行交互,为什么不在其他构造函数中引用主窗口呢?

public class MainForm : Form
{
}

public class OtherForm : Form
{
    protected MainForm MainForm { get; set; }

    public OtherForm(MainForm mainForm) : base()
    {
        this.MainForm = mainForm;
    }
}

EDIT: 编辑:

Simple and effective. 简单有效。

If your forms need to interact with all the other forms of the application, then a service locator type pattern might be better suited. 如果您的表单需要与应用程序的所有其他表单进行交互,则服务定位器类型模式可能更适合。

Previously with MFC, there was something that notify all windows. 以前使用MFC,有些东西会通知所有窗口。 You will pass an event id with a parameter. 您将传递带有参数的事件ID。

You could do something similar with one delegate that will expose an event id and a collection of parameter. 您可以对一个委托执行类似的操作,该委托将公开一个事件ID和一组参数。

The greatest advantage of this is that windows only have to implement one gateway. 这样做的最大好处是Windows只需实现一个网关即可。

您可以使用单个委托,使用自定义EventArgs传递多个信息,例如:通知类型,其他参数等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM