我想了解如何使用 Windows 窗体在 .net 上实现 MVP 模式。将来我想在网络上使用创建的模式。
我的问题是我不确定我是否做得对。
我所做的是一些演示者,我将多个视图附加到它,这意味着如果不首先指定附加到它的所有视图,我就无法使用该演示者。
public class ScorePresenter{
private IScoreView _scoreView;
private IClientView _clientView;
public ScorePresenter()
{
}
public void AttachView(IScoreView view){
this._scoreView = view;
}
public void AttachView(IClientView view){
this._clientView = view;
}
public void Create(Model model){
try{
//create code here
this._clientView.Reload();
}
catch(Exception ex){
}
}
}
public class ClientPresenter(){
private IClientView _clientView;
public ClientPresenter(){
}
public void AttachView(IClientView view){
this._clientView = view;
}
}
public interface IClientView{
void Reload();
}
public interface IScoreView{
}
用法
客户端表单 vb.net
Public Class ClientForm
Implements IClientView
Private _clientPresenter As ClientPresenter
Public Sub ClientForm_Load() Handles Me.Load
Me._clientPresenter = new ClientPresenter()
Me._clientPresenter.AttachView(Me)
End Sub
Public Sub Reload Implements IClientView.Reload
Reload code here
End Sub
Public Sub ScoreButton_Click() Handles ScoreButton.Click
Dim frmScoreForm as New ScoreForm
frmScoreForm.MyParent = Me
frmScoreForm.ShowDialog()
End Sub
End Class
评分表 vb.net
Public Class ScoreForm
Implements IScoreView
Private _scorePresenter As ScorePresenter
Public Sub ScoreForm_Load() Handles Me.Load
Me._scorePresenter = new ScorePresenter()
Me._scorePresenter.AttachView(Me)
Me._scorePresenter.AttachView(Me._myParent)
End Class
Private _myParent as Object
Public WriteOnly Property MyParent As Object
Set(value As Object)
Me._myParent = value
End Set
End Property
End Class
在此代码客户端表单上是主表单,如果我单击客户端表单上的分数按钮,它将显示分数表单
在分数表上,如果我在其中创建或操作数据,它将调用客户端表单重新加载,客户端表单也将更新视图上的数据
我在这个中看到的是我不能单独使用 ScorePresenter 对吗?这是一个糟糕的设计吗?如果是,还有其他方法可以实现我想要发生的事情吗?