C# 视窗表格及;设计模式

C# 视窗表格及;设计模式,c#,design-patterns,C#,Design Patterns,我更熟悉mvc、mvp或mvvm模式。所以我在谷歌上搜索,想为win-form应用程序实现良好的设计模式。我发现了很多文章。很少有人说mvc是好的,也很少有人说mvp是win应用程序的完美选择。我发现了一个在win应用程序中实现mvp的非常小的代码。我仔细阅读了代码,发现开发人员必须编写大量额外的代码来绑定treeview或任何控件 代码如下所示 public interface IYourView { void BindTree(Model model); } public class

我更熟悉mvc、mvp或mvvm模式。所以我在谷歌上搜索,想为win-form应用程序实现良好的设计模式。我发现了很多文章。很少有人说mvc是好的,也很少有人说mvp是win应用程序的完美选择。我发现了一个在win应用程序中实现mvp的非常小的代码。我仔细阅读了代码,发现开发人员必须编写大量额外的代码来绑定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);
   }
}

请有人通读代码,帮助我理解流程,因为我不知道如何用mvp模式编写代码。谢谢。

此代码已经在使用MVP模式

它声明了一个接口
IYourView
和一个具体的类
YourView
,该类实现System.Windows.Form和这个新接口。基本上,它所做的是创建一个新表单,并要求它还实现
IYourView
中定义的
BindTree()
方法

然后,
YourView
类(表单)有一个
YourPresenter
的依赖项,以便将
OnLoad
事件连接到presenter,尽管我会在presenter订阅表单的
OnLoad
事件的地方执行此操作

presenter
YourPresenter
将YourView的一个实例作为依赖项,然后可以在其其余逻辑中使用该实例

现在,要使用它,您将遵循与以下类似的过程:

  • 创建
    YourView
    的新实例(然后创建演示者)
  • 在演示器中实现逻辑(即-create
    GetModel()
    ),以创建要绑定到树的模型
  • 在演示者中调用
    view.BindTree(model)
    ,其中model是您在上一步中刚刚创建的
因此,创建视图的实例:

IYourView newView = new YourView();
然后在演示者类中:

Model model = GetModel();
newView.BindTree(model);

请重新格式化-不要使用
,而是在每行代码前插入4个空格。我最近一直在学习winforms模式,希望理解,但我无法理解您的问题。。。