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

本文是小编为大家收集整理的关于Windows窗体的UI设计模式(像WPF的MVVM)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

MVVM最常用于WPF,因为它非常适合它.但是Windows表格呢? Windows表单是否也有类似且常用的方法/设计模式?一个与Windows表单明确效果很好?是否有一本书或文章来描述这一点?也许基于MVP或MVC?

推荐答案

我尝试过MVP,并且似乎也可以很好地与Windows表单一起使用. 本书有一个带有MVP模式的Windows表单的示例(示例薪资应用程序).该应用程序不是那么复杂,但会让您了解如何创建它.

c# ...

您可以在 源代码

编辑:

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

对于复杂的数据指标方案,我更喜欢使用监督控制器模式. 在监督控制器模式时,数据指示责任与视图有关.因此,对于TreeView/DataGrid,这应该在各自的视图中,只有视图不可知逻辑才能转移到主持人中.

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

不要以名称为单位(这是MVP框架).

简单的Winforms MVP视频 winforms -mvp

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

简单的树景绑定示例(穷人的绑定).您可以在Bindtree()中添加任何TreeView特定逻辑.

下面是代码段.................................

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);
   }
}

其他推荐答案

正如它已经说过的,使用Winforms时,我总是以MVP模式工作.但是,您将使用的设计模式并不意味着您将正确使用. MVP上有大量的抗模式.

如果您想以良好的方式启动所有内容,则必须使用框架来构建智能客户端.因此,我建议使用该设计和实践:智能客户端软件工厂 http://.codeplex.com/smartclient

您在这里讨论当前的智能客户端框架: http://codebetter.com/blogs/glenn.blocks/glock/archive/2008/05/10/prism-cab-cab-and-winforms-futures.aspx

ps:我喜欢MVP抗patterns上的这篇文章: http:http:http:http:http:http://blog.mattwynne.net/2007/06/13/mvp-smells/

希望这会有所帮助

其他推荐答案

model-view-viewModel(mvvm)模式是一种设计模式.根据定义,设计模式显示了面向对象的世界中的常见解决方案,并且该解决方案可以应用于各种平台(WPF,Winforms,Java Swing等).我同意MVVM最好与WPF一起使用,因为它利用了强大的绑定功能.但是,Windows表单也支持数据绑定.

waf windows findows findows fornes fornes apapter 显示如何显示如何显示如何显示如何显示在Windows表单应用程序中应用MVVM模式.

本文地址:https://www.itbaoku.cn/post/627485.html

问题描述

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?

推荐答案

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.

Agile Principles, Patterns, and Practices in C#...

You can get the source code at Source Code

EDIT:

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.

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

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

Simple winforms MVP video Winforms - MVP

An example of dealing with dropdown list MVP - DropDownList

Simple treeview binding example (poor man's binding). You can add any treeview specific logic in BindTree().

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);
   }
}

其他推荐答案

As it has already said, i always worked in a MVP pattern when using Winforms. But the design pattern you will use not mean you will use right. There is loads of anti-pattern attached to MVP.

If you want to starts everything in a good manner, you have to use the framework for building smart client. So i will recommend to use that design and practices: Smart Client Software Factory http://www.codeplex.com/smartclient

You have a discussion here about the current smart client frameworks : http://codebetter.com/blogs/glenn.block/archive/2008/05/10/prism-cab-and-winforms-futures.aspx

PS: I like this post on the MVP anti-patterns: http://blog.mattwynne.net/2007/06/13/mvp-smells/

Hope this helps

其他推荐答案

The Model-View-ViewModel (MVVM) Pattern is a design pattern. Per definition a design pattern shows a common solution in the object-oriented world and this solution can be applied in various platforms (WPF, WinForms, Java Swing, etc.). I agree that MVVM is best used with WPF because it leverages the strong binding capabilities. However, Windows Forms supports data binding as well.

The WAF Windows Forms Adapter shows how to apply the MVVM Pattern in a Windows Forms application.

相关标签/搜索