简体   繁体   English

MVP-从视图到演示者获取数据

[英]MVP - Getting Data from the View to the Presenter

I'm trying to get some practice implementing the MVP pattern in a simple C# WinForms application. 我正在尝试一些在简单的C#WinForms应用程序中实现MVP模式的练习。 On the left of the view is a tree view with a list of the files saved by the application; 视图的左侧是树视图,其中包含应用程序保存的文件列表。 on the right of the view is a DataGridView for displaying whichever file is clicked in the tree view, or for typing into to save as a new file. 视图右侧是DataGridView,用于显示在树视图中单击的文件,或键入以另存为新文件。 The files are simply Dictionary objects written to disk with BinaryFormatter. 这些文件只是使用BinaryFormatter写入磁盘的Dictionary对象。

I created an interface for the view: 我为视图创建了一个接口:

public interface IMappingsView
{
    event EventHandler SaveMapping;
    event EventHandler NewMapping;
    event EventHandler<DeleteArgs> DeleteMapping;
    event EventHandler PasteData;
    event EventHandler NodeClicked;
}

The delete button on the view has the following click event handler: 视图上的删除按钮具有以下单击事件处理程序:

private void buttonDeleteMapping_Click(object sender, EventArgs e)
{
    var node = treeView1.SelectedNode.Text;
    var args = new DeleteArgs(Path.Combine(RootDir,node));

    if (DeleteMapping != null)
    {
        DeleteMapping(this, args);
        dataGridView1.Rows.Clear();
        RefreshTreeView();
    }
}

What is the best way to to pass information from the view to the presenter? 将信息从视图传递到演示者的最佳方法是什么? I feel as though needing custom event arguments for every scenario is very wrong. 我觉得对于每种情况都需要自定义事件参数是非常错误的。

Make the data you want available available via the interface as a property. 通过界面将所需的数据作为属性提供。

Assuming you have a firstName and lastName field that you want exposed... 假设您有一个要公开的名字和姓氏字段...

public interface IMappingsView
{
    event EventHandler SaveMapping;
    event EventHandler NewMapping;
    event EventHandler<DeleteArgs> DeleteMapping;
    event EventHandler PasteData;
    event EventHandler NodeClicked;
    string FirstName {get;set;}
    string LastName {get;set;}
}

Then in your form that implements the interface, 然后以实现该接口的形式,

string FirstName {
    get {
        return textFirstName.Text;
    }
    set { 
        textFirstName.Text = value;
    }
}

as an example. 举个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM