如何在Windows窗体上使用模式MVP(被动)为用户控件定义正确的体系结构?

时间:2018-09-13 15:03:05

标签: c# mvp

我选择了模式MVP(被动)。这是我在MVP中的第一个实现,我对此有很多疑问。我已经看过很少的网站,也没有看视频,但这还不足以让我知道如何进行。

我的视图中有三个控件,但一次只能显示一个。演示者 ConfigureCurrentWindow(FieldType type)中有一个方法,该方法负责获取类型并更新视图以显示正确的控件。一次只能显示一个控件。

我选择的方式使用方法(ConfigureCurrentWindow&Focus)更新视图。我选择了正确的实现还是打破了架构MVP(被动)?

在我的脑海中将有两个演讲者和两个观点。一位演示者负责用户控制,另一位演示者向用户显示表单。这样对吗?

我在某些站点中看到应该使用事件处理程序来进行用户交互。为什么在我的视图中不能调用类似“ _presenter.TextChanged()”的方法?

如何从正确的控件中获取“文本”值?我应该在哪里实现获取值的方法?

模型

public interface IInputDialogModel : IModel
{
    FieldType Type { get; set; }

    string Description { get; set; }

    string Text {get;set;}
    //I've omitted some properties 

    void OnValueChanged(Object sender, InputDialogEventArgs e);
    event EventHandler<InputDialogEventArgs> ValueChanged;

    void OnValueCompleted(Object sender, InputDialogEventArgs e);
    event EventHandler<InputDialogEventArgs> ValueCompleted;
}

查看

public interface IInputDialogView : IView
{

    string Title { get; set; }

    string InputText { get; set; }

    string Description { get; set; }

    bool Required { get; set; }
    //I've omitted some properties

    event EventHandler<EventArgs> InputValueChanged;

    void Focus(FieldType type);

    void SetErrorProvider(string message);

    void ClearErrorProvider();

}

混凝土视图

public partial class InputDialogForm : Form, IInputDialogView
{
    private InputDialogModalPresenter _presenter;
    #region Interface
    public event EventHandler<EventArgs> InputValueChanged;


    public string Title { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public bool MultiLine { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public FormBorderStyle BorderStyle { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    public string InputText { get => throw new NotImplementedException(); 
    //I've omitted some properties
    #endregion


    public InputDialogForm(IInputDialogModel inputDialogModel)
    {
        InitializeComponent();
        _presenter = new InputDialogModalPresenter(this, inputDialogModel);
        maskDate.TextChanged += txtInput_TextChanged;
    }

    private void txtInput_TextChanged(object sender, EventArgs e)
    {
        //InputValueChanged?.Invoke(sender, e);
    }
    private void InputDialogForm_Shown(object sender, EventArgs e)
    {
        _presenter.SetFocus();
    }

    void IInputDialogModalView.Focus(FieldType type)
    {
    //I must choose which control must be focused according the Model Type???
        switch (fieldType)
        {
            case FieldType.SingleLine:
                textboxSingle.Focus();
                break;
            case FieldType.MultiLine:
                textboxMulti.Focus();
                break;
            case FieldType.Date:
                maskedDate.Focus();
                break;
        }
    }

    //My principal question is here. I must turn visible the correct panel 
    //by the Model Type. is this implementation right?
    public void ConfigureCurrentWindow(FieldType fieldType)
    {
        Panel currentPanel = null;

        switch (fieldType)
        {
            case FieldType.SingleLine:
                 currentPanel = panelSingleTextBox;
                break;
            case FieldType.MultiLine:
                currentPanel = panelMultiTextBox;
                break;
            case FieldType.Date:
                currentPanel = panelDate;
                break;
        }
        currentPanel.Visible = true;
        currentPanel.Dock = DockStyle.Fill;
    }

}

演示者

public class InputDialogPresenter : Presenter<IInputDialogView, IInputDialogModel>{

public InputDialogModalPresenter(IInputDialogModalView view, IInputDialogModel model) :base(view , model)
{
    abstractFactoryInputDialog  =AbstractFactoryInputDialog.GetFactory(view, model);
    LoadInputDialog(view, model);

}

protected override void SubscribeEvents()
{
    View.InputValueChanged += OnValueChanged;
}

public void Focus()
{
    view.Focus(model.Type);
}

public void UpdateView()
{
    view.ConfigureCurrentWindow(model.Type);
}}

有关UserControl的图片

https://imgur.com/8kJUQdn

0 个答案:

没有答案