【问题标题】:How to set control state in MVP如何在 MVP 中设置控制状态
【发布时间】:2016-02-18 09:42:44
【问题描述】:

当用户无法引发事件时,我想禁用按钮(或其他控件)。做这个的最好方式是什么?视图句柄或演示者应在视图中按属性传递值,然后视图将更新控件的状态。

例如,如果之前的查询没有完成,用户不应该开始新的。

选项 1:

interface IView
{
    event EventHandler Event;
}

class View : IView
{
    private readonly Button _button;

    public event EventHandler Event;

    public void button_Click(object sender, EventArgs e)
    {
        _button.Enabled = false;

        if(Event != null)
        {
            Event(this, EventArgs.Empty);
        }

        _button.Enabled = true;
    }

}

class Presenter
{
    public void View_Event(object sender, EventArgs e)
    {
        // code...
    }
}

选项 2:

interface IView
{
    event EventHandler Event;

    bool CanRaiseEvent { set; }
}

class View : IView
{
    private readonly Button _button;

    public event EventHandler Event;

    public bool CanRaiseEvent
    {
        set
        {
            _button.Enabled = value;
        }
    }

    public void button_Click(object sender, EventArgs e)
    {
        if (Event != null)
        {
            Event(this, EventArgs.Empty);
        }
    }
}

class Presenter
{
    private readonly IView _view;
    public void View_Event(object sender, EventArgs e)
    {
        _view.CanRaiseEvent = false;

        // code...

        _view.CanRaiseEvent = true;
    }
}

我知道我应该在执行下一个查询之前检查演示者查询的状态,但我想通知视图用户甚至不应该尝试。

【问题讨论】:

  • 在我看来逻辑应该在演示者内部。所以选项 2 更好。

标签: c# winforms design-patterns mvp


【解决方案1】:

我用于 MVP 设计的两个“试金石”测试是:1) 逻辑是否可测试? 2)我可以替换具体视图并且应用程序仍然可以工作吗?

从这个角度来看,选项 2 看起来更有吸引力。

【讨论】:

    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多