在Interfaces中引发事件 [英] Raising events in Interfaces

查看:104
本文介绍了在Interfaces中引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用MVP模式实现一个项目,我有一些这样的接口:

  public interface IWizardStep { 
event EventHandler NextStep;
}

public interface ISubmitable {
event EventHandler Submit;
}

公共接口IStep:ISubmitable,IWizardStep {
string SomeProperty {get;组; }
}

我的演示者课程有这个:

  public class StepPresenter {
public IStep View {get;组; }

public StepPresenter(){
this.View.Submit + = new EventHandler(StepSubmitted);
}

void StepSubmitted(对象发送者,EventArgs e){
//做一些处理

//现在我想提出Next事件
//this.View.NextStep(sender,e)抛出编译错误
}

}

因此,当我尝试在View中引发NextStep事件时,我遇到编译错误。我很确定这是一个问题,因为我没有为NextStep实现一个事件处理程序,但是这个演示者不会处理这个事件。



演示者将在窗体上的向导中实现为许多步骤(ASP.NET,但没有任何区别),向导将捕获NextStep事件并显示向导中的下一步。 / p>

那么如何才能在这样的界面上发生事件,或者这是不可能的?

解决方案

这是因为NextStep是一个eventHandler。您不能调用必须引发的事件处理程序。我希望IStep有一个方法调用,比如MoveToStep(),具体实现会按预期引发事件,你可以订阅其他地方。



你是什么推理IWizardStep中的NextStep eventHandler?如果它是你正在使用的方法,那么在你的界面而不是事件中创建它。你的消费者类应该期望在需要做某事时调用一个方法,并在IWizardStep有东西要传回时订阅一个事件(数据或其他)


I'm implementing a project in MVP pattern and I've got some interfaces like this:

public interface IWizardStep {
  event EventHandler NextStep;
}

public interface ISubmitable {
  event EventHandler Submit;
}

 public interface IStep : ISubmitable, IWizardStep {
  string SomeProperty { get; set; }
 }

My presenter class then has this:

public class StepPresenter {
  public IStep View { get; set; }

  public StepPresenter() {
    this.View.Submit += new EventHandler(StepSubmitted);
  }

  void StepSubmitted(object sender, EventArgs e){
    //do some processing

    //Now I want to raise the Next event
    //this.View.NextStep(sender, e) throws a compile error
  }

}

So I'm getting a compile error when trying to raise the NextStep event on the within the View. I'm pretty sure that it's a problem because I haven't implemented an event handler for NextStep, but this presenter wont be handling the event.

The presenter will be implemented as 1 step of many in a wizard on a form (ASP.NET, but that doesn't make any difference) and the wizard will catch the NextStep event and show the next step in the wizard.

So how can I have an event on an interface raised like this, or is it impossible?

解决方案

This is because NextStep is an eventHandler. You cannot call an event handler which must be raised. I would expect IStep to have a method call such as MoveToStep() which the concrete implementation would raise the event as expected which you could subscribe to somewhere else.

What is your reasoning for the NextStep eventHandler in IWizardStep? If its a methd you're after, create that in your interface instead of an event. Your consumer class should expect to call a method when needing to "do" something and subscribe to an event when the IWizardStep has something to pass back (data or whatever)

这篇关于在Interfaces中引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆