gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:16:33 25 4
gpt4 key购买 nike

假设您有一个包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com