概括
我正在 Windows 窗体应用程序中试验 MVP 模式。
我想让我的 Presenters 和 Views 平台不可知,所以如果我想将我的应用程序移植到另一个平台,比如 Web 或移动设备,我只需要使用平台相关的 GUI 实现视图,我的 Presenters 仍然可以独立于平台。
现在我想知道,如何使用 MVP 和被动视图来 ShowDialog()?
到目前为止,据我所知,被动视图不应该知道/关心任何演示者。他们甚至不知道它的存在。因此,根据我的说法,这个问题的答案中提出的解决方案不合适: Refactoring Form.ShowDialog() code to MVP
一些代码示例有助于理解:
ApplicationView
public partial class ApplicationForm : Form, IApplicationView {
// I ensure that all the IApplicationView events are raised
// upon button clicks text changed, etc.
// The presenter, which this view ignores the existence,
// is subscribed to the events this view raises.
}
ApplicationPresenter
public class ApplicationPresenter
: Presenter<IApplicationView>
, IApplicationPresenter {
public ApplicationPresenter(IApplicationView view) : base(view) {
View.OnViewShown += OnViewShown;
}
public void OnViewShown() {
IAuthenticaitonView authView = new AuthenticationForm();
IAuthenticationPresenter authPresenter = new AuthenticationPresenter(authView);
authPresenter.ShowDialog(); // 1.
}
}
- 这就是我挣扎的地方。ApplicationPresenter 就像 Universer 中的主人,可能通过 和 知道用户身份
IAuthenticationView
验证IAuthenticationPresenter
。
IAuthenticationView
public interface IAuthenticationView : IDialogView {
string ErrorMessage { get; set; }
string Instance { get; set; }
IEnumerable<string> Instances { get; set; }
string Login { get; set; }
string Password {get; set; }
void EnableConnectButton(bool enabled);
event VoidEventHandler OnConnect;
event SelectionChangedEventHandler OnDatabaseInstanceChanged;
event VoidEventHandler OnLoginChanged;
event VoidEventHandler OnPasswordChanged;
}
IDialogView
public interface IDialogView : IView {
void ShowDialog();
}
IView
public interface IView {
void Show();
event VoidEventHandler OnViewInitialize;
event VoidEventHandler OnViewLoad;
event VoidEventHandler OnViewShown;
}
IAuthenticationPresenter
public interface IAuthenticationPresenter : IPresenter<IAuthenticationView> {
void OnConnect();
void OnViewDatabaseInstanceChanged(SelectionChangedEventArgs e);
void OnViewLoginChanged();
void OnViewPasswordChanged();
}
IPresenter<V>
public interface IPresenter<V> where V : IView {
V View { get; }
OnViewInitialize();
OnViewLoad();
ShowView();
}