C# winforms mvp 在演示器中显示子窗体
本文关键字:显示 窗体 winforms mvp | 更新日期: 2023-09-27 17:57:19
我使用了 MVP 设计父,我MainForm(MDI)
我想在父 mdi 容器中显示子窗体。
我的问题是:
将初始化子窗体的代码放在view
或presenter
中是否很好?
Mehtod 从调用演示器方法的视图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();
}
如果您使用的是 MVP 父项,则在演示者中显示子窗体。这是最好的解决方案。不要像这样称呼演示者
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.
}