Windows 窗体的 UI 设计模式(如 WPF 的 MVVM) [英] UI Design Pattern for Windows Forms (like MVVM for WPF)

查看:24
本文介绍了Windows 窗体的 UI 设计模式(如 WPF 的 MVVM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVVM 最常与 WPF 一起使用,因为它非常适合它.但是 Windows 窗体呢?对于 Windows 窗体,是否也有这样的既定且常用的方法/设计模式?一个可以很好地与 Windows Forms 配合使用的吗?有没有一本书或一篇文章很好地描述了这一点?也许基于 MVP 或 MVC?

MVVM is most commonly used with WPF because it is perfectly suited for it. But what about Windows Forms? Is there an established and commonly used approach / design pattern like this for Windows Forms too? One that works explicitly well with Windows Forms? Is there a book or an article that describes this well? Maybe MVP or MVC based?

推荐答案

我尝试过 MVP,它似乎对 Windows 窗体也能很好地工作.本书有一个带有 MVP 模式的 Windows 窗体示例(示例工资单应用程序).该应用程序并不复杂,但可以让您了解如何创建它.

I have tried MVP and it seems to work great with windows forms too. This book has an example of windows forms with MVP pattern (sample payroll application). The application is not that complex but will give you an idea about how to go about creating it.

C# 中的敏捷原则、模式和实践...

您可以在以下位置获取源代码源代码

You can get the source code at Source Code

MVP 模式有两种变体(a) 被动视图和 (b) 监督控制器

There are two variations of the MVP pattern (a) Passive view and (b) supervising controller

对于复杂的数据绑定场景,我更喜欢使用监督控制器模式.在监督控制器模式中,数据绑定的责任在于视图.因此,对于树视图/数据网格,这应该在各自的视图中,只有与视图无关的逻辑才应移至演示者.

For complex databinding scenarios I prefer to go with the Supervising controller pattern. In supervising controller pattern the databinding responsibility rest with the view. So,for treeview/datagrid this should be in the respective views, only view agnostic logic should moved on to the presenter.

我建议您查看以下 MVP 框架MVC# - MVP 框架

I'll recommend having a look at the following MVP framework MVC# - An MVP framework

不要直呼其名(这是一个 MVP 框架).

Don't go by the name (it's an MVP framework).

简单的 winforms MVP 视频Winforms - MVP

Simple winforms MVP video Winforms - MVP

处理下拉列表的例子MVP - DropDownList

下面是代码片段....未测试,直接从思想中键入....

Below is the code snippet.... not tested, directly keyed in from thought....

public interface IYourView
{
   void BindTree(Model model);
}

public class YourView : System.Windows.Forms, IYourView
{
   private Presenter presenter;

   public YourView()
   {
      presenter = new YourPresenter(this);
   }

   public override OnLoad()
   {
         presenter.OnLoad();
   }

   public void BindTree(Model model)
   {
       // Binding logic goes here....
   }
}

public class YourPresenter
{
   private IYourView view;

   public YourPresenter(IYourView view)
   { 
       this.view = view;
   }

   public void OnLoad()
   {
       // Get data from service.... or whatever soruce
       Model model = service.GetData(...);
       view.BindTree(model);
   }
}

这篇关于Windows 窗体的 UI 设计模式(如 WPF 的 MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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