讓我們假設一個主窗體有幾個菜單項。每個菜單項都會顯示另一種形式。下面的代碼是在這種情況下執行依賴注入的好方法嗎?在具有子表單的WinForms應用程序中執行依賴注入
public class MainForm: Form {
private IAboutForm _aboutForm;
private IOptionsForm _optionsForm;
private IDownloadsForm _downloadsForm;
MainForm(IAboutForm aboutForm, IOptionsForm optionsForm, IDownloadsForm downloadsForm) { // add as many form dependencies as required
_aboutForm = aboutForm;
_optionsForm = optionsForm;
_downloadsForm = downloadsForm;
InitializeComponent();
}
private void AboutMenuItem_Click(object sender, System.EventArgs e) {
_aboutForm.ShowDialog(this);
}
private void DownloadsMenuItem_Click(object sender, System.EventArgs e) {
_downloads.Show();
_downloads.BringToFront();
}
}
@Gjeltema,是的! :) – fxam 2013-05-13 01:32:02