最佳实践传递Windows.Forms的之间的信息 [英] Best practice to pass information between windows.forms

查看:150
本文介绍了最佳实践传递Windows.Forms的之间的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你是干什么的传递形式之间的信息?前锋是直线前进(对不起)使用新的()或DoStuff()方法的属性或者参数,但对于送点东西的返回 当用户与完成第二种形式? (IE项目的ID选择)我们用这些:

What do you do to pass information between forms? Forward is straight forward (sorry) using Properties or maybe parameters in a New() or DoStuff() method, but what about sending something back when the user is done with the second form? (IE. ID of the item selected) We have used all these:

  • 作为一个裁判已通过调用形式进入所谓的形式这样的所谓形式可以在调用的形式访问属性或方法。我真的不喜欢这一点,因为这两种形式都非常依赖对方。传递调用形式为对象只是稍微提高了这一点。
  • 使用活动这个有点分离了code,但签名必须在事件处理程序相匹配。
  • 使用一个公共接口我说的是建于一体的.NET,但我想你可以创建自己的。这似乎是最好的我。
  • Passed the calling form into the called form as a ref so the called form could access properties or methods on the calling form. I really don't like this because the two forms are very dependent of each other. Passing the calling form as a object only slightly improves this.
  • Use Events This somewhat decouples the code, but the signatures must match on the event handler.
  • Use an Public Interface I'm talking about the .NET built in one, but I suppose you could create your own. This seems like the best to me.

现在提高标准,如果形式是在两个不同的DLL文件是什么?只要形式是不依赖于对方,我想这不会是一大步。

Now raise the bar, what if the forms are in two different DLLs? As long as the forms are not dependent on each other, I would think this wouldn't be a big step.

推荐答案

我发现,一旦你有一个精心设计的域实体对象模型或干脆业务对象。这些任务变得更加容易。

I have found that once you have a well designed Domain Entity Object Model or simply business objects. These tasks become much easier.

如果你没有域实体,如员工,客户,地点等,你会发现自己写的形式一堆属性,打造吨尴尬的依赖关系。随着时间的推移这可能是非常混乱。

If you dont have Domain Entities such as Employee, Account, Location etc., you find yourself writing forms with a bunch of properties and create tons of awkward dependencies. Over time this can be very messy.

一旦你的域实体代替的东西更容易对付。例如,使用一个表格,你可以简单地创建一个这样的员工属性编辑自己的员工:

Once you have the Domain Entity in place things are much easier to deal with. For example, to edit your Employee using a form you can simply create an Employee property like this:

NewForm myForm = new NewForm();     		
myForm.Employee = employeeToEdit; // This can have state 
myForm.ShowDialog(); 
Employee editedEmployee= myform.Employee;

EmployeeFacade.SaveEmployee(editedEmployee); // Or whatever

关于活动,作为WinForm / WPF应用服务也几乎跳投有助于创建使用发布/订阅模式来处理表单之间通信的全球eventmanager进行这是非常罕见的我永远不会有一个形式'谈'直接到任何其他形式。这是另一个话题,所以我不会细讲,如果你想的例子,我可以提供几个我已经做了。

Regarding Events, for Winform/WPF apps it it almost aways helpful to create a global EventManager using publish / subscribe pattern to handle communication between forms. It is very rare I will ever have one form 'talk' directly to any other form. That is another topic so I will not go into detail, if you want examples I can provide several I have done.

瑞艾芙德布鲁克希尔

这篇关于最佳实践传递Windows.Forms的之间的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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