Skip to main content
 首页 » 编程设计

wpf之WPF 中使用的设计模式

2025年05月04日136think

以下摘自this article在 MVVM 上。有人可以提供这些其他模式(命令、DI)如何在 WPF 中使用的示例吗? WPF 中是否还有其他您未在此处列出的设计模式?

There are other patterns you should be aware of to assist you in MVVM. Patterns like commanding (baked into WPF, solutions for SL), mediator and gasp dependency injection. .

请您参考如下方法:

我写过关于其中一些的文章:WPF and Silverlight design patterns

以下是模式的简要说明:

1) MVVM - 用作模型转换器和代码隐藏的替代品。提高可测试性,为 ViewModel 编写单元测试要容易得多。

2) 依赖注入(inject) - 用于提高类的可测试性(您可以为特定类与其他类分开编写单元测试)以及以更简单的方式更改实现的可能性(更改记录器、缓存提供程序、Web 服务等)

3) 命令 - 默认情况下可以应用于 Button 和 MenuItem 控件,如果无法执行操作,则禁用控件。也用于 MVVM 模式作为代码隐藏事件的替代。

经典书籍中已在 WPF 中使用的其他模式:

  • 辛格尔顿。 WPF中的Application类和HttpContext类
    在 Web 表单中。
  • 适配器。使用 IValueConverter 的数据绑定(bind)引擎
    为 UI 转换绑定(bind)值的接口(interface)。
  • 装饰师。装饰任何 UIElement 类的 Border 类
    具有可变厚度和颜色的边框。
  • 正面。 PrintDialog 类,它提供了一个简单的接口(interface)
    使您能够使用整个打印和文档子系统
    WPF 提供的。
  • 命令。 ICommand 接口(interface),由
    RoutedCommand 和 RoutedUICommand 类。
  • 迭代器。 IEnumerator 接口(interface),其中包含许多集合和列表
    在 .NET Framework 中实现。
  • 观察者。 INotifyPropertyChanged 接口(interface)和事件。