c# - 在事件链接时消除顺序依赖性和冗余的干净方法是什么?

标签 c# winforms oop events

假设您有一个包含 3 个字段的对话框。

  • 案例 1. 输入字段 1 将默认字段 2 和字段 3 (事件处理程序 1)
  • 案例 2。输入字段 2 将默认字段 3 (事件处理程序 2)
  • 案例 3。输入字段 3 不默认任何内容。

这有两个问题:

  1. 冗余:无需额外努力,eventHandler1 隐式 触发 eventHandler2。在这个玩具示例中,这不是问题。 但是将场景扩展到更多的领域并且没有 小心,开销会变得巨大。

  2. 顺序依赖性:我认为字段 3 将默认使用 eventHandler2。 但无论哪种情况;有时,eventHandler1 的默认值可能是 正确的。其他时候,eventHandler2 的默认值可能是正确的。

是否有一种简洁、结构化的方式在 C# 中处理此问题而无需处理大量状态?

最佳答案

好的,您需要做的是在 winforms 应用程序中分离图层。设计一个类来确定哪些字段影响哪些其他字段。此类中的每个属性都将使用属性 setter 中的自定义代码来修改依赖属性,这又会向窗口发送一个信号,表明它们的内容已更改。

“View”(View-like)类将成为第一个类的容器,处理输入事件,调用第二个类的方法来处理结果。最后,当其他类发出命令时,它会更新其他字段。

旧的,不正确的答案(没有捕获 Winforms 标签,愚蠢的我

The simplest answer is good View-Model and View seperation, but without a code sample, it's hard to determine if MVVM is appropriate.

So your View xaml will have definitions for three (four, ten, whatever) fields, each databound to a property in your ViewModel.

The setter for each property will handle the logic of setting dependant values in other properties. As each property is set, they should notify when changed, and the UI will update without any further work from you.

Much less coupled; much more structured.

关于c# - 在事件链接时消除顺序依赖性和冗余的干净方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183037/

相关文章:

c# - 用 [SomeAttribute] 和 [Some] 装饰类的区别

c# - SSL + Jquery + Ajax

c# - 通过Datatable在DataGridView中添加Combobox

c# - IE8、Flash 和 WinForms WebBrowser 控件

Javascript oop 疑问

c# - 如何在页面加载时隐藏 devexpress gridview 中的列?

c# - 如何根据其值动态更改 asp 标签的 CSS 类

c# - 在自动运行时隐藏主窗体 C#

c# - 你能解释一下这种泛型行为吗?如果我有解决方法?

java - 为对象创建特定的时间戳?