Skip to content

Instantly share code, notes, and snippets.

@thedersen
Created August 4, 2010 15:33
Show Gist options
  • Save thedersen/508315 to your computer and use it in GitHub Desktop.
Save thedersen/508315 to your computer and use it in GitHub Desktop.
public interface IPresenter
{
void Initialize();
object UI { get; }
}
public interface IMyPresenter : IPresenter
{
}
public interface IMyPresenterCallbacks
{
void OnSave();
void OnMyTextChanged();
}
public class MyPresenter : IMyPresenter, IMyPresenterCallbacks
{
private IMyView _view;
public MyPresenter(IMyView view)
{
_view = view;
}
public object UI
{
get { return _view; }
}
public void Initialize()
{
_view.Attach(this);
_view.SaveButtonText = "Save";
_view.SaveButtonEnabled = false;
}
public void OnSave()
{
// Save _view.MyText
}
public void OnMyTextChanged()
{
_view.SaveButtonEnabled = !string.IsNullOrEmpty(_view.MyText);
}
}
public interface IView<TCallbacks>
{
void Attach(TCallbacks presenter);
}
public interface IMyView : IView<IMyPresenterCallbacks>
{
string MyText { get; set; }
string SaveButtonText { get; set; }
bool SaveButtonEnabled { get; set; }
}
public class MyView : Form, IMyView
{
private Button _saveButton;
private TextBox _myTextBox;
public void Attach(IMyPresenterCallbacks callback)
{
_saveButton.Click += (sender, e) => callback.OnSave();
_myTextBox.TextChanged += (sender, e) => callback.OnMyTextChanged();
}
public string MyText
{
get { return _myTextBox.Text; }
set { _myTextBox.Text = value; }
}
public string SaveButtonText
{
get { return _saveButton.Text; }
set { _saveButton.Text = value; }
}
public bool SaveButtonEnabled
{
get { return _saveButton.Enabled; }
set { _saveButton.Enabled = value; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment