我正在学习将MVP应用于C#中一个简单的WinForms应用程序(只有一种形式),并在static void Main()
中创建主要演示者时遇到问题。为了将它作为参数提供给Application.Run(),从Presenter公开一个View是否是一个好主意?应如何为MVP WinForms应用程序的主要演示者调用Application.Run()?
目前,我实现了一个办法,让我不暴露查看演示的属性:
static void Main()
{
IView view = new View();
Model model = new Model();
Presenter presenter = new Presenter(view, model);
presenter.Start();
Application.Run();
}
启动和停止方法主讲人:
public void Start()
{
view.Start();
}
public void Stop()
{
view.Stop();
}
开始和视图中的Stop方法(Windows窗体):
public void Start()
{
this.Show();
}
public void Stop()
{
// only way to close a message loop called
// via Application.Run(); without a Form parameter
Application.Exit();
}
Application.Exit()调用似乎是关闭窗体(和应用程序)的一种不雅的方式。另一种方法是将View作为Presenter的公共属性公开,以便使用Form参数调用Application.Run()。
static void Main()
{
IView view = new View();
Model model = new Model();
Presenter presenter = new Presenter(view, model);
Application.Run(presenter.View);
}
Presenter中的启动和停止方法保持不变。额外的属性添加到返回查看表单:
public void Start()
{
view.Start();
}
public void Stop()
{
view.Stop();
}
// New property to return view as a Form for Application.Run(Form form);
public System.Windows.Form View
{
get { return view as Form(); }
}
的启动和停止方法在View(Windows窗体)将被写成如下:
public void Start()
{
this.Show();
}
public void Stop()
{
this.Close();
}
任何人都可以建议哪些是更好的方法,为什么?或者有更好的方法来解决这个问题?
我感谢罗杰,Heinzi和妮可为自己宝贵的意见,但我选择了Heinzi的答案终于因为我已经适应了他的答案用两个额外的接口IMainPresenter和IMainView我的实际应用。 – anonymous 2010-05-12 03:29:39