gpt4 book ai didi

c# - 如何使用 MVP 和被动 View Form.ShowDialog()?

转载 作者:行者123 更新时间:2023-11-30 17:48:47 26 4
gpt4 key购买 nike

总结

我正在 Windows 窗体应用程序中试验 MVP 模式。

我想让我的 Presenter 和 Views 平台不可知,所以如果我想将我的应用程序移植到另一个平台,比如 Web 或移动平台,我只需要使用依赖于平台的 GUI 和我的 Presenter 来实现 View 仍然可以独立于平台。

现在我想知道,如何使用 MVP 和被动 View 来 ShowDialog()?

据我目前的理解,被动 View 不应该了解/关心任何演示者。他们甚至不知道它的存在。因此,根据我的说法,这个问题的答案中提出的解决方案是不合适的: 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.
}
}
  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();
}

最佳答案

基于这些前提:

  • 演示者应与平台无关
  • 只有 View 知道如何显示/展示自己(WPF、Mobile、Silverlight、Web、WinForms...)
  • View 必须提供一种显示自身的方式
  • View 不必与平台无关,因为显示会因平台而异
  • 但演示者应命令 View 何时显示自己

我是这样想的:

IView

public interface IView {
void OnShowView();
}

IPresenter<V>

public interface IPresenter<V>where V : IView {
void ShowView();
event VoidEventHandler OnShowView;
}

Presenter<V>

public abstract class Presenter<V> : IPresenter<V> {
public Presenter(V view) {
View = view;
OnShowView += View.OnShowView;
}

public void ShowView() { raiseShowViewEvent(); }

public event VoidEventHandler OnShowView;

private void raiseShowViewEvent() { if (OnShowView != null) OnShowView(); }
}

所以,按照我到目前为止所遇到的逻辑,我通过这样做解决了它:

ApplicationForm

public partial class ApplicationForm : Form, IApplicationView {
private void ApplicationForm_Shown(object sender, EventArgs e) { raiseOnViewShown(); }

private void raiseOnViewShownEvent() { if (OnViewShown != null) OnViewShown(); }
}

ApplicationPresenter

public void OnViewShown() {
// This method is the subscriber of the IView.OnViewShown event
// The event is raised with the ApplicationForm_Shown event.
IAuthenticationView authView = new AuthenticationForm();
IAuthenticationPresenter authPresenter = new AuthenticationPresenter(authView);
authPresenter.ShowView(); // 1.
}
  1. 这引发了 OnShowView IAuthenticationView 已订阅的事件。然后,返回表单, View 对事件的响应是:

AuthenticationForm

public partial class AuthenticationForm : Form, IAuthenticationView {
public void OnShowView() { ShowDialog(); }
}

然后, View 将自身显示为对话框/模态窗口。

关于c# - 如何使用 MVP 和被动 View Form.ShowDialog()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com