想象一下帶有一個TextBox(CustomerName)和兩個按鈕(Save/Cancel)的簡單UserControl。更改模型
該用戶控件位於與兩個按鈕(PreviousCustomer/NextCustomer)
問題是我不知道在哪裏的模型演示者傳遞一個父控件中。我是否通過視圖構造函數?如果模型更改(單擊了PreviousCustomer/NextCustomer),該怎麼辦?每次創建新視圖嗎?如果UserControl對於很多孩子來說很複雜,這看起來很浪費。我可以在視圖和演示者中實現UpdateModel方法嗎,還是不正確?
這裏是我的代碼:
public interface IModel
{
string CustomerName { get; set; }
}
public class Presenter
{
private IView _view;
private IModel _model;
public Presenter(IView view, IModel model)
{
_view = view;
_model = model;
_view.CustomerName = _model.CustomerName;
}
public void Save()
{
_model.CustomerName = _view.CustomerName;
}
public void Cancel()
{
_view.CustomerName = _model.CustomerName;
}
public void UpdateModel(IModel newModel)
{
_model = newModel;
_view.CustomerName = _model.CustomerName;
}
}
public interface IView
{
string CustomerName { get; set; }
}
public class View : UserControl, IView
{
private Presenter _presenter;
public string CustomerName
{
get { return CustomerNameEditBox.Text; }
set { CustomerNameEditBox.Text = value; }
}
//Is this the right place to pass in the model?
public View(IModel model)
{
_presenter = new Presenter(this, model);
}
private void SaveButton_Click(object sender, EventArgs e)
{
_presenter.Save();
}
private void CancelButton_Click(object sender, EventArgs e)
{
_presenter.Cancel();
}
public void UpdateModel(IModel newModel)
{
_presenter.UpdateModel(newModel);
}
}
編輯:由於在下面的評論說,我認爲這是最好有觀點是完全啞一無所知演示或模型。這是我修改的代碼。
型號:
public interface IModel
{
string CustomerName { get; set; }
}
public class Model : IModel
{
public string CustomerName { get; set; }
public Model(string name)
{
CustomerName = name;
}
}
查看:
public interface IView
{
event EventHandler SaveClick;
event EventHandler CancelClick;
string CustomerName { get; set; }
}
public class View : UserControl, IView
{
public event EventHandler SaveClick
{
add { SaveButton.Click += value; }
remove { SaveButton.Click -= value; }
}
public event EventHandler CancelClick
{
add { CancelButton.Click += value; }
remove { CancelButton.Click -= value; }
}
public string CustomerName
{
get { return CustomerNameEditBox.Text; }
set { CustomerNameEditBox.Text = value; }
}
}
主持人:
public class Presenter
{
private IView _view;
private IModel _model;
public Control View
{
get { return (Control)_view; }
}
public Presenter(IView view, IModel model)
{
_view = view;
_view.SaveClick += new EventHandler(_view_SaveClick);
_view.CancelClick += new EventHandler(_view_CancelClick);
_model = model;
_view.CustomerName = _model.CustomerName;
}
void _view_SaveClick(object sender, EventArgs e)
{
_model.CustomerName = _view.CustomerName;
}
void _view_CancelClick(object sender, EventArgs e)
{
_view.CustomerName = _model.CustomerName;
}
public void ChangeModel(IModel newModel)
{
_model = newModel;
_view.CustomerName = _model.CustomerName;
}
}
用法:
public class MainProgram : Form
{
public void ArbitraryMethod()
{
//Create a new model object
IModel model1 = new Model("test1");
//Create the presenter with the model and any view implementing IView
Presenter presenter = new Presenter(new View(), model1);
//Add the view to a panel control in this form
mainPanel.Controls.Add(presenter.View);
//Change the model
IModel model2 = new Model("test2");
presenter.ChangeModel(model2);
}
}
誰調用Save和Cancel方法?這是觀點嗎? –
是的,查看有SaveButton_Click/CancelButton_Click方法在演示者中調用保存/取消方法。 – BoroDrummer