C# 如何在MVP中将事件参数从视图传递给演示者?

C# 如何在MVP中将事件参数从视图传递给演示者?,c#,winforms,event-handling,mvp,C#,Winforms,Event Handling,Mvp,我有一个基于MVP、WinForms和EntityFramework的应用程序。 在一个表单中,我需要验证单元格值,但我不知道如何将EventArgs从DataGridView的验证事件传递给演示者 我有这个表格(不相关代码省略): 接口: public interface IChargeLinePropertiesView:IView { event Action CellValidating; DataGridView BudgetDataGrid { get; } } 这

我有一个基于MVP、WinForms和EntityFramework的应用程序。 在一个表单中,我需要验证单元格值,但我不知道如何将EventArgs从DataGridView的验证事件传递给演示者

我有这个表格(不相关代码省略):

接口:

public interface IChargeLinePropertiesView:IView
{
    event Action CellValidating;
    DataGridView BudgetDataGrid { get; }
}
这位演讲者:

public class ChargeLinePropertiesPresenter : BasePresenter<IChargeLinePropertiesView, ArgumentClass>
{
    public ChargeLinePropertiesPresenter(IApplicationController controller, IChargeLinePropertiesView view)
        : base(controller, view)
    {
        View.CellValidating += View_CellValidating;
    }

    void View_CellValidating()
    {
        //I need to validate cell here based on dgBudget.CellValidating EventArgs
        //but how to pass it here from View?

        //typeof(e) == DataGridViewCellValidatingEventArgs
        //pseudoCode mode on
        if (e.FormattedValue.ToString() == "Bad")
        {
            View.BudgetDataGrid.Rows[e.RowIndex].ErrorText =
                "Bad Value";
            e.Cancel = true;
        }
        //pseudoCode mode off
    }
}
void View_CellValidating(object sender, CancelEventArgs e)
{
   //...
   if (nothappy) e.Cancel = true;
}
公共类ChargeLinePropertiesPresenter:BasePresenter
{
公用ChargeLinePropertiesPresenter(IApplicationController控制器,IChargeLinePropertiesView视图)
:底座(控制器、视图)
{
View.CellValidating+=视图\单元验证;
}
void视图_CellValidating()
{
//我需要根据dgBudget在此验证单元格。CellValidating EventArgs
//但如何从视图中传递它呢?
//typeof(e)=DataGridViewCellValidatingEventArgs
//上的伪码模式
如果(例如,FormattedValue.ToString()=“坏”)
{
View.BudgetDataGrid.Rows[e.RowIndex].ErrorText=
“不良价值”;
e、 取消=真;
}
//伪码模式关闭
}
}
是的,我可以通过接口公开一个属性,并将我的EventArgs设置为该属性,以便从Presenter获取它们,但这很难看,不是吗

public interface IChargeLinePropertiesView:IView
{
    event Action CellValidating;
    // etc..
}
使用
操作
是这里的问题,它是错误的委托类型。它不允许传递任何参数。解决此问题的方法不止一种,例如,您可以使用
Action
。但逻辑选择是使用验证事件使用的相同委托类型:

event CancelEventHandler CellValidating;
现在很容易。以您的形式:

public event CancelEventHandler CellValidating;

public ChargeLinePropertiesForm() {
    InitializeComponent();
    dgBudget.CellValidating += (sender, cea) => {
        var handler = CellValidating;
        if (handler != null) handler(sender, cea);
    };
}
在演示者中:

public class ChargeLinePropertiesPresenter : BasePresenter<IChargeLinePropertiesView, ArgumentClass>
{
    public ChargeLinePropertiesPresenter(IApplicationController controller, IChargeLinePropertiesView view)
        : base(controller, view)
    {
        View.CellValidating += View_CellValidating;
    }

    void View_CellValidating()
    {
        //I need to validate cell here based on dgBudget.CellValidating EventArgs
        //but how to pass it here from View?

        //typeof(e) == DataGridViewCellValidatingEventArgs
        //pseudoCode mode on
        if (e.FormattedValue.ToString() == "Bad")
        {
            View.BudgetDataGrid.Rows[e.RowIndex].ErrorText =
                "Bad Value";
            e.Cancel = true;
        }
        //pseudoCode mode off
    }
}
void View_CellValidating(object sender, CancelEventArgs e)
{
   //...
   if (nothappy) e.Cancel = true;
}