1
iがMVPは
public interface IProgressView
{
string Status { set; }
void SetProgress(int percentageDone);
void Display();
void Close();
event Action Closing;
}
class ProgressPresenter
{
private IProgressView m_view;
private ILongRunningTask m_task;
private bool m_completed;
public Progress(IProgressView view)
{
m_view = view;
}
public virtual void Display(ILongRunningTask task, string taskName)
{
m_task = task;
m_view.Status = taskName " is running";
m_view.Closing += OnClosing;
task.ProgressChanged += UpdateProgress;
task.Completed += Completed;
task.StartAsync();
m_view.Display();
m_view.Closing -= OnClosing;
task.ProgressChanged -= UpdateProgress;
task.Completed -= Completed;
}
protected virtual void UpdateProgress(object sender, ProgessEventArgs e)
{
m_view.SetProgress(e.AlreadyDone * 100/e.Total);
}
protected virtual void Completed()
{
m_completed = true;
m_view.Status = "Completed";
m_view.Close();
}
private virtual void OnClosing()
{
if (!m_completed) m_downloader.Cancel();
}
}
私の問題は、タスクが別のスレッドで実行されていることで、簡単なProgressPresenterとビューへの各呼び出し(Form
として実装)スローを実装WinFormsの。私はそれが別のスレッドから呼び出すことができ念の
public string Status
{
set { Invoke(new Action(() => progressLabel.Text = value)); }
}
状に各メソッドをラップする必要がありますか?またはプレゼンターに欠陥がありますか?
アドバイスありがとうございます
THXの例えば
:私はこれらのものを持っています。両方のオプションは非常に便利です。 ATM私は本当のIOCに依存したくないので、私は自分自身を非常に単純なものにしたので、今私は2番目のオプションになっています – Firo
Postsharpをプロキシのような側面の代わりに使うことができます! –