经过3个独立的WinForms之间的数据 [英] Passing Data Between 3 Separate WinForms

查看:210
本文介绍了经过3个独立的WinForms之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的时间在C#中的WinForms的境界编码,我有许多实例中,我所遇到的形式之间传递数据的不同方法。我在一个大的代码库工作 - 其中一些方法被别人,这是我后来扩展,和其他人我自己写的写的。它似乎有两个主要的范式,这两者我有编码相当舒适。实例或显示子窗体时

Throughout my time coding in the realm of C# WinForms, I have had many instances in which I have come across different methods of passing data between forms. I work on a large codebase -- some of these methods were written by others, which I subsequently extended, and others were written by myself. It seems there are two main paradigms, both of which I have coded rather comfortably.

1)通过父窗体给孩子。例如:

1.) Pass the parent form to the child when instantiating or showing the child form. For example:

    ChildForm.Instance = new ChildForm(this);

    --Or--

    ChildForm.Instance = new ChildForm();
    ChildForm.Instance.Show(this.TopLevelControl);

这让孩子从父提取信息,以及允许家长在调用方法这个孩子。和我一起承担了一会儿 - 我不知道,这打破如此多的范例,是坏的做法 - 记住,我维护一个更大的代码库的过程中遇到很多这其中,我需要做增量调整而不完整地重构。

This allows the child to pull information from the parent, as well as allows the parent to call methods on the child. Bear with me for a moment -- I do realize that this breaks so many paradigms, and is "bad" practice -- remember, I'm encountering much of this during maintenance of a larger codebase to which I am required to make incremental adjustments without doing a complete refactoring.

2)使用事件委托,以允许与父母和孩子之间的形式传输数据。据我所知,这仍需要父窗体产卵孩子时得设立此事件。例如:

2.) Use an event delegate to allow for data to be transferred between parent and child forms. To the best of my knowledge, this still requires that the parent form establish this event when spawning the child. For example:

在父:

    childForm = new ChildForm(this);
    DataRead += new DataReadEventHandler(childForm.ChildForm_DataRead);



在小孩:

Within child:

    public void ChildForm_DataRead(Data data)
    {
         if (InvokeRequired)
         {
              Invoke(new MethodInvoker(delegate() { ChildForm_DataRead(data); }));
         }
         else
              //do something
    }

这种性质的东西。现在,我还没有在C#的WinForms强大的编码器,但我不知道该事件/消息的方法是从设计的角度来看可能是好。

Something of this nature. Now, I'm still not a strong coder in C# WinForms, but I do realize that the event/messaging approach is probably "better" from a design perspective.

现在,这里是我的问题。

Now, here is my question.

我有一个主要形式,命名的缘故。ParentForm。 ParentForm目前采用后一种形式(HAR HAR!)消息来传递数据,让我们说,FirstChildForm。从本质上讲,一旦ParentForm采集数据,它触发DataReadEventHandler和数据从ParentForm传递到FirstChildForm。

I have a main form, for naming's sake: ParentForm. ParentForm currently utilizes the latter form (har har!) of messaging to pass data to, let's say, FirstChildForm. Essentially, once ParentForm acquires data, it triggers the DataReadEventHandler, and data is passed from ParentForm to FirstChildForm.

没有问题。

现在,I /也/具有ParentForm催生一种形式,叫做SecondChildForm。注:这是不是ChildForm的另一个实例...这是一个完全不同的形式。这里的渔获 - 当SecondChildForm数据更新,我想有这样的数据传递给FirstChildForm。这似乎是这样一个简单的想法,虽然我有一些困难,我的包裹围绕如何实现它的头。所有我能想到的是从ParentForm为每个孩子建立独特的事件处理程序,并具有SecondChildFrom事件处理程序然后触发ParentForm的事件处理程序FirstChildForm ......这听起来非常令人费解,因为数据(不平凡的大小,我可以补充),必须首先从SecondChildForm传递到ParentForm,并随后从ParentForm到FirstChildForm。

Now, I /also/ have a form spawned from ParentForm, called SecondChildForm. NB: this is not another instance of ChildForm... it's an entirely different form. Here's the catch -- when data updates on SecondChildForm, I want to have this data passed to FirstChildForm. It seems like such a simple idea, although I'm having some difficulty wrapping my head around how to implement it. All I can think of is setting up unique event handlers from ParentForm for each child, and having the event handler from SecondChildFrom then trigger ParentForm's event handler for FirstChildForm... this sounds terribly convoluted, as the data (of non-trivial size, I might add), must be first passed from SecondChildForm to ParentForm, and subsequently from ParentForm to FirstChildForm.

有没有这个做什么?

另外,我真的不想这么说,但是,坦白说,在这个高度封闭的应用,我和打破范例为简单确定,如果正确的方法是非常令人费解(不过会在未来适当的重构分配时间 - !是的,其实我是/ AM /能够做到这一点)!

Also, I'd really prefer not to say this, but, to be perfectly honest, in this highly closed application, I'm OK with breaking paradigm for simplicity if the proper method is highly convoluted (would nevertheless allocate time in the future for proper refactoring -- yes, I actually /am/ able to do this!).

干杯

-Kadaj

推荐答案

所以数据首先对第二个孩子产生的,所以在该形成我们会想,可以触发一个事件,可以提供数据:

So the data is first generated on the second child, so on that form we'll want an event that can be triggered that can provide that data:

public class SecondChildForm : Form
{
    public event Action<MyData> SomethingHappened;

    //Other code, including code that fires the event at some point
}


$触发事件代码b $ b

然后我们必须有一些需要调用传递数据方法的第一个孩子:

Then we have the first child which has some method that needs to be called passing in that data:

public class FirstChildForm : Form
{
    public void WhenSomethingHappens(MyData data)
    {
        //Do stuff with data
    }
}

最后,我们有一个既创造的形式和电线相应的事件处理程序的主要形式有:

Finally we have the main form that creates both of the forms and wires up the appropriate event handlers:

public class ParentForm : Form
{
    public ParentForm()
    {
        FirstChildForm firstChild = new FirstChildForm();
        SecondChildForm secondChild = new SecondChildForm();

        secondChild.SomethingHappened += firstChild.WhenSomethingHappens;

        //show forms and do other stuff
    }
}

瞧。

请注意,使用这种模式每一个孩子不知道他们的父母什么。他们通过公开事件由家长需要的信息,而且它们允许家长通过公开方式影响它,但他们不知道或不关心哪一个类(ES)的使用它们。父的确实的了解其子类型;这是适合它有特定的子类的实例,并操纵它的直接公共成员(而不是其内在的控制,这不应该是公共的)。

Note that using this pattern each child doesn't know anything about their parent. They expose information needed by the parent through events, and they allow the parent to affect it through public methods, but they don't know or care which class(es) are using them. The parent does know about it's child type; it's appropriate for it to have an instance of the specific child type and to manipulate it's public members (but not its inner controls, which shouldn't be public) directly.

这篇关于经过3个独立的WinForms之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆