C# 关于事件的问题
我对使用C#事件处理非常陌生。我们的应用程序定义了以下事件:C# 关于事件的问题,c#,C#,我对使用C#事件处理非常陌生。我们的应用程序定义了以下事件: public sealed class MonitorCallback : IMonitorCallback { public event EventHandler<ApplyEventArgs> ApplyAccepted; public event EventHandler<ApplyEventArgs> ApplyRejected; } 公共密封类监视器回调:IMonitorCallba
public sealed class MonitorCallback : IMonitorCallback
{
public event EventHandler<ApplyEventArgs> ApplyAccepted;
public event EventHandler<ApplyEventArgs> ApplyRejected;
}
公共密封类监视器回调:IMonitorCallback
{
公共事件处理程序ApplyAccepted;
公共事件事件处理程序ApplyRejected;
}
我需要编写一些代码来处理和响应这些事件。有人能告诉我如何做吗?当您开始键入下面的
+=
并点击选项卡时,Visual Studio将自动为事件处理程序函数创建存根
protected MyMonitorCallback MonitorCallback;
public class MyClass
{
void Main()
{
MyMonitorCallback = new MonitorCallback();
MyMonitorCallback.ApplyAccepted += new EventHander<ApplyEventArgs>(MyMonitorCallback_ApplyAccepted);
}
void MyMonitorCallback_ApplyAccepted(object sender, ApplyEventArgs e) {
..
}
}
受保护的MyMonitorCallback MonitorCallback;
公共类MyClass
{
void Main()
{
MyMonitorCallback=新建MonitorCallback();
MyMonitorCallback.ApplyAccepted+=新事件处理程序(MyMonitorCallback\u ApplyAccepted);
}
void MyMonitorCallback\u ApplyAccepted(对象发送方,ApplyEventArgs e){
..
}
}
当您开始键入下面的+=
并单击选项卡时,Visual Studio将自动为事件处理程序函数创建存根
protected MyMonitorCallback MonitorCallback;
public class MyClass
{
void Main()
{
MyMonitorCallback = new MonitorCallback();
MyMonitorCallback.ApplyAccepted += new EventHander<ApplyEventArgs>(MyMonitorCallback_ApplyAccepted);
}
void MyMonitorCallback_ApplyAccepted(object sender, ApplyEventArgs e) {
..
}
}
受保护的MyMonitorCallback MonitorCallback;
公共类MyClass
{
void Main()
{
MyMonitorCallback=新建MonitorCallback();
MyMonitorCallback.ApplyAccepted+=新事件处理程序(MyMonitorCallback\u ApplyAccepted);
}
void MyMonitorCallback\u ApplyAccepted(对象发送方,ApplyEventArgs e){
..
}
}
最好从msdn开始
这将通过声明、调用和连接事件来完成
阅读委托()也是一个好主意,因为您将在事件处理中使用委托(),最好从一开始就了解它。最好从msdn开始 这将通过声明、调用和连接事件来完成
阅读委托()可能也是一个好主意,因为您将在事件处理中使用委托(),并且最好从头开始理解委托。当您创建
MonitorCallback
实例时,您将事件订阅到要编写的事件处理程序。语法看起来像
public class MonitorCallbackFactory{
public MonitorCallback CreateCallback(){
// create the callback instance
var callback = new MonitorCallback();
// subscribe the events to the EventHandler.
callback.ApplyAccepted += OnApplyAccepted;
callback.ApplyRejected += OnApplyRejected;
return callback;
}
protected virtual void OnApplyAccepted(object sender, ApplyEventArgs e){
// the sender is always the type of object that raises the event, so
// if you need it strongly typed you can do:
var callback = (MonitorCallback)sender;
// then write your code for what happens when
// the ApplyAccepted event is raised here
}
protected virtual void OnApplyRejected(object sender, ApplyEventArgs e){
// write your code for what happens when
// the ApplyRejected event is raised here
}
}
如您所见,
+=
是订阅事件处理程序的语法-=
是取消订阅和事件处理程序的语法 创建MonitorCallback
实例时,将事件订阅给要编写的事件处理程序。语法看起来像
public class MonitorCallbackFactory{
public MonitorCallback CreateCallback(){
// create the callback instance
var callback = new MonitorCallback();
// subscribe the events to the EventHandler.
callback.ApplyAccepted += OnApplyAccepted;
callback.ApplyRejected += OnApplyRejected;
return callback;
}
protected virtual void OnApplyAccepted(object sender, ApplyEventArgs e){
// the sender is always the type of object that raises the event, so
// if you need it strongly typed you can do:
var callback = (MonitorCallback)sender;
// then write your code for what happens when
// the ApplyAccepted event is raised here
}
protected virtual void OnApplyRejected(object sender, ApplyEventArgs e){
// write your code for what happens when
// the ApplyRejected event is raised here
}
}
如您所见,
+=
是订阅事件处理程序的语法-=
是取消订阅和事件处理程序的语法 我想您已经有了这样的代码
public class EventExample
{
private EventHandler<ApplyEventArgs> m_evApplyAccepted;
public event EventHandler<ApplyEventArgs> ApplyAccepted
{
add { m_evApplyAccepted += value; }
remove { m_evApplyAccepted -= value; }
}
protected virtual void OnEventName(ApplyEventArgs e)
{
if (m_evApplyAccepted != null)
m_evApplyAccepted.Invoke(this, e);
}
public class ApplyEventArgs : EventArgs { }
}
public class EventConsumer
{
private EventExample eventExample;
public EventConsumer()
{
eventExample = new EventExample();
//register a handler with the event here
eventExample.ApplyAccepted += new EventHandler<EventExample.ApplyEventArgs>(eventExample_EventName);
}
void eventExample_EventName(object sender, EventExample.ApplyEventArgs e)
{
//respond to event here
}
}
公共类事件示例
{
私有事件处理程序m_evApplyAccepted;
公共事件处理程序ApplyAccepted
{
添加{m_evApplyAccepted+=value;}
删除{m_evApplyAccepted-=value;}
}
受保护的虚拟void OnEventName(ApplyVentArgs e)
{
如果(m_evApplyAccepted!=null)
m_evApplyAccepted.Invoke(this,e);
}
公共类ApplyEventArgs:EventArgs{}
}
你这样消费这个事件
public class EventExample
{
private EventHandler<ApplyEventArgs> m_evApplyAccepted;
public event EventHandler<ApplyEventArgs> ApplyAccepted
{
add { m_evApplyAccepted += value; }
remove { m_evApplyAccepted -= value; }
}
protected virtual void OnEventName(ApplyEventArgs e)
{
if (m_evApplyAccepted != null)
m_evApplyAccepted.Invoke(this, e);
}
public class ApplyEventArgs : EventArgs { }
}
public class EventConsumer
{
private EventExample eventExample;
public EventConsumer()
{
eventExample = new EventExample();
//register a handler with the event here
eventExample.ApplyAccepted += new EventHandler<EventExample.ApplyEventArgs>(eventExample_EventName);
}
void eventExample_EventName(object sender, EventExample.ApplyEventArgs e)
{
//respond to event here
}
}
公共类事件消费者
{
私人事件示例;
公共事件消费者()
{
eventExample=新的eventExample();
//在此处向事件注册处理程序
eventExample.ApplyAccepted+=新的EventHandler(eventExample\u EventName);
}
void eventExample\u EventName(对象发送方,eventExample.ApplyEventArgs e)
{
//在这里回应事件
}
}
我想您已经有了这样的代码
public class EventExample
{
private EventHandler<ApplyEventArgs> m_evApplyAccepted;
public event EventHandler<ApplyEventArgs> ApplyAccepted
{
add { m_evApplyAccepted += value; }
remove { m_evApplyAccepted -= value; }
}
protected virtual void OnEventName(ApplyEventArgs e)
{
if (m_evApplyAccepted != null)
m_evApplyAccepted.Invoke(this, e);
}
public class ApplyEventArgs : EventArgs { }
}
public class EventConsumer
{
private EventExample eventExample;
public EventConsumer()
{
eventExample = new EventExample();
//register a handler with the event here
eventExample.ApplyAccepted += new EventHandler<EventExample.ApplyEventArgs>(eventExample_EventName);
}
void eventExample_EventName(object sender, EventExample.ApplyEventArgs e)
{
//respond to event here
}
}
公共类事件示例
{
私有事件处理程序m_evApplyAccepted;
公共事件处理程序ApplyAccepted
{
添加{m_evApplyAccepted+=value;}
删除{m_evApplyAccepted-=value;}
}
受保护的虚拟void OnEventName(ApplyVentArgs e)
{
如果(m_evApplyAccepted!=null)
m_evApplyAccepted.Invoke(this,e);
}
公共类ApplyEventArgs:EventArgs{}
}
你这样消费这个事件
public class EventExample
{
private EventHandler<ApplyEventArgs> m_evApplyAccepted;
public event EventHandler<ApplyEventArgs> ApplyAccepted
{
add { m_evApplyAccepted += value; }
remove { m_evApplyAccepted -= value; }
}
protected virtual void OnEventName(ApplyEventArgs e)
{
if (m_evApplyAccepted != null)
m_evApplyAccepted.Invoke(this, e);
}
public class ApplyEventArgs : EventArgs { }
}
public class EventConsumer
{
private EventExample eventExample;
public EventConsumer()
{
eventExample = new EventExample();
//register a handler with the event here
eventExample.ApplyAccepted += new EventHandler<EventExample.ApplyEventArgs>(eventExample_EventName);
}
void eventExample_EventName(object sender, EventExample.ApplyEventArgs e)
{
//respond to event here
}
}
公共类事件消费者
{
私人事件示例;
公共事件消费者()
{
eventExample=新的eventExample();
//在此处向事件注册处理程序
eventExample.ApplyAccepted+=新的EventHandler(eventExample\u EventName);
}
void eventExample\u EventName(对象发送方,eventExample.ApplyEventArgs e)
{
//在这里回应事件
}
}
虽然我不反对帮助您,但您能否询问您团队中编写它们的人?虽然我不反对帮助您,但您能否询问您团队中编写它们的人?EventHander代表还具有发件人属性。。。因此,您的代码应该是MyMonitorCallback\u ApplyAccepted(object sender,ApplyEventTargets e)。EventHander委托还具有sender属性。。。因此,您的代码应该是MyMonitorCallback\u ApplyAccepted(对象发送方,ApplyEventArgs e)