【问题标题】:C# winforms mvp show child form in presenterC# winforms mvp 在演示者中显示子窗体
【发布时间】:2016-07-16 17:50:40
【问题描述】:

我使用了 MVP 设计模式,我 MainForm(MDI) 我想在父 mdi 容器中显示子表单。

我的问题是:

将初始化子表单的代码放在viewpresenter 中是一种好习惯吗?

从视图中调用演示者方法ShowMusicLibrary的方法:

查看

public void ShowLibraryButton_Click(object sender, EventArgs e)
{
    this.Presenter.ShowMusicLibrary();

    // put here or in presenter
}

演讲者:

    public void ShowMusicLibrary()
    {
        this._model.ShowAll();
    }

初始化子表单的代码在这里:

 foreach (Form topList in Application.OpenForms)
 {
     if (topList.GetType() == typeof(MusicLibrary))
     {
          form.Activate();
          return;
     }

     _ms = new MusicLibrary();
     _ms.MdiParent = this;
     _ms.Show(); 
   }

【问题讨论】:

    标签: c# winforms mvp


    【解决方案1】:

    如果您使用 MVP 模式,则在 Presenter 中显示子窗体。 这是最好的解决方案。 不要像

    那样直接调用presenter
      this.Presenter.ShowMusicLibrary();
    

    使用将在演示者上实现的事件创建接口,并在 yotu 将在单击视图按钮后显示子视图时调用。 有这样的想法:

    intercafe IShowMusicLibrary
    {
    event action show();
    }
    
    //view
    
    
    public void ShowLibraryButton_Click(object sender, EventArgs e)
    {
       if(show!=null)
    show();
    
    
    }
    
    //presenter
    private IShowMusicLibrary _musicLibrary;
    
    // in constructor
    _musicLibrary.show += YourEvent;
    
    
    public YourEvent()
    {
    //here show a child control.
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 2019-09-27
      • 1970-01-01
      相关资源
      最近更新 更多