我正在學習將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