使用自定义事件访问器在c#中添加事件时如何调用方法?
我正在读这个 我不清楚该怎么做使用自定义事件访问器在c#中添加事件时如何调用方法?,c#,.net,C#,.net,我正在读这个 我不清楚该怎么做 更新:因为有人已经回答了这些主持人如何假装这个问题是模糊的 下面是一个简单的示例,说明如何处理自定义事件访问器,我知道您希望在每次从事件附加/分离事件处理程序时调用函数。在这种情况下,您需要在下面的代码中创建一个私有后备委托myCustomEventDelegate,并让您的自定义事件访问器add/remove阻止向委托添加/删除处理程序,当然还需要调用其他功能 在本例中,我只是编写控制台,当然,如果您的代码是库的一部分,可以用于可能无法访问控制台的不同类型的应
更新:因为有人已经回答了这些主持人如何假装这个问题是模糊的 下面是一个简单的示例,说明如何处理自定义事件访问器,我知道您希望在每次从事件附加/分离事件处理程序时调用函数。在这种情况下,您需要在下面的代码中创建一个私有后备委托
myCustomEventDelegate
,并让您的自定义事件访问器add/remove
阻止向委托添加/删除处理程序,当然还需要调用其他功能
在本例中,我只是编写控制台,当然,如果您的代码是库的一部分,可以用于可能无法访问控制台的不同类型的应用程序,那么这不是一个好主意,但毕竟这只是一个示例
using System;
namespace CustomEventDemo
{
class Program
{
static void Main(string[] args)
{
TheClassExposingYourEvent instance = new TheClassExposingYourEvent();
instance.MyCustomEvent += new EventHandler<EventArgs>(Program_MyCustomEvent);
instance.DoSomething();
instance.MyCustomEvent -= new EventHandler<EventArgs>(Program_MyCustomEvent);
Console.ReadKey();
}
static void Program_MyCustomEvent(object sender, EventArgs e)
{
Console.WriteLine("The event was fired");
}
}
class TheClassExposingYourEvent
{
private EventHandler<EventArgs> _myCustomEventDelegate;
public event EventHandler<EventArgs> MyCustomEvent
{
add
{
_myCustomEventDelegate += value;
// Do something extra here.
// Writing to the console is a bad example!!!
Console.WriteLine("Event handler attached");
}
remove
{
_myCustomEventDelegate -= value;
// Do something extra here.
// Writing to the console is a bad example!!!
Console.WriteLine("Event handler detached");
}
}
public void DoSomething()
{
if (_myCustomEventDelegate != null)
{
_myCustomEventDelegate(this, EventArgs.Empty);
}
}
}
}
使用系统;
命名空间CustomEventDemo
{
班级计划
{
静态void Main(字符串[]参数)
{
ClassExposingYourEvent实例=新建ClassExposingYourEvent();
instance.MyCustomEvent+=新的EventHandler(程序\u MyCustomEvent);
实例DoSomething();
instance.MyCustomEvent-=新的EventHandler(程序\u MyCustomEvent);
Console.ReadKey();
}
静态无效程序\u MyCustomEvent(对象发送方,事件参数e)
{
Console.WriteLine(“事件被触发”);
}
}
将ClassExposingYourEvent分类
{
私有事件处理程序\u myCustomEventDelegate;
公共事件处理程序MyCustomEvent
{
添加
{
_myCustomEventDelegate+=值;
//在这里做些额外的事情。
//写控制台是一个糟糕的例子!!!
Console.WriteLine(“附加事件处理程序”);
}
去除
{
_myCustomEventDelegate-=值;
//在这里做些额外的事情。
//写控制台是一个糟糕的例子!!!
WriteLine(“事件处理程序分离”);
}
}
公共无效剂量测定法()
{
如果(_myCustomEventDelegate!=null)
{
_myCustomEventDelegate(此,EventArgs.Empty);
}
}
}
}
下面是一个简单的示例,说明如何处理自定义事件访问器,我知道您希望在每次从事件附加/分离事件处理程序时调用函数。在这种情况下,您需要在下面的代码中创建一个私有后备委托myCustomEventDelegate
,并让您的自定义事件访问器add/remove
阻止向委托添加/删除处理程序,当然还需要调用其他功能
在本例中,我只是编写控制台,当然,如果您的代码是库的一部分,可以用于可能无法访问控制台的不同类型的应用程序,那么这不是一个好主意,但毕竟这只是一个示例
using System;
namespace CustomEventDemo
{
class Program
{
static void Main(string[] args)
{
TheClassExposingYourEvent instance = new TheClassExposingYourEvent();
instance.MyCustomEvent += new EventHandler<EventArgs>(Program_MyCustomEvent);
instance.DoSomething();
instance.MyCustomEvent -= new EventHandler<EventArgs>(Program_MyCustomEvent);
Console.ReadKey();
}
static void Program_MyCustomEvent(object sender, EventArgs e)
{
Console.WriteLine("The event was fired");
}
}
class TheClassExposingYourEvent
{
private EventHandler<EventArgs> _myCustomEventDelegate;
public event EventHandler<EventArgs> MyCustomEvent
{
add
{
_myCustomEventDelegate += value;
// Do something extra here.
// Writing to the console is a bad example!!!
Console.WriteLine("Event handler attached");
}
remove
{
_myCustomEventDelegate -= value;
// Do something extra here.
// Writing to the console is a bad example!!!
Console.WriteLine("Event handler detached");
}
}
public void DoSomething()
{
if (_myCustomEventDelegate != null)
{
_myCustomEventDelegate(this, EventArgs.Empty);
}
}
}
}
使用系统;
命名空间CustomEventDemo
{
班级计划
{
静态void Main(字符串[]参数)
{
ClassExposingYourEvent实例=新建ClassExposingYourEvent();
instance.MyCustomEvent+=新的EventHandler(程序\u MyCustomEvent);
实例DoSomething();
instance.MyCustomEvent-=新的EventHandler(程序\u MyCustomEvent);
Console.ReadKey();
}
静态无效程序\u MyCustomEvent(对象发送方,事件参数e)
{
Console.WriteLine(“事件被触发”);
}
}
将ClassExposingYourEvent分类
{
私有事件处理程序\u myCustomEventDelegate;
公共事件处理程序MyCustomEvent
{
添加
{
_myCustomEventDelegate+=值;
//在这里做些额外的事情。
//写控制台是一个糟糕的例子!!!
Console.WriteLine(“附加事件处理程序”);
}
去除
{
_myCustomEventDelegate-=值;
//在这里做些额外的事情。
//写控制台是一个糟糕的例子!!!
WriteLine(“事件处理程序分离”);
}
}
公共无效剂量测定法()
{
如果(_myCustomEventDelegate!=null)
{
_myCustomEventDelegate(此,EventArgs.Empty);
}
}
}
}
不是这样吗
event EventHandler IDrawingObject.OnDraw
{
add
{
lock (PreDrawEvent)
{
PreDrawEvent += value;
}
YourFunction(); // HERE
}
不是这样吗
event EventHandler IDrawingObject.OnDraw
{
add
{
lock (PreDrawEvent)
{
PreDrawEvent += value;
}
YourFunction(); // HERE
}
事件实际上是非公共委托,由add/remove访问器包装,以限制外部类修改委托的方式。下面的代码虽然冗长,但对委托和事件进行了详细解释。它来自我为试图学习.Net的同事们准备的一个示例项目。像读一本书一样自上而下地阅读(添加注释用于解释,但代码是编译的):
//http://msdn.microsoft.com/en-us/library/aa288459(第71节)
//委托很像函数指针,事件“看起来”很像委托。
//从某种意义上说,委托是一个函数指针类。下面是一个示例声明
//具有int返回值和两个参数(bool、string)的委托的
公共委托int MyDelegate(bool abool,字符串astring);
//幕后代理实际上属于System.MulticastDelegate类型,因此
//可以有多个调用。
//看http://msdn.microsoft.com/en-us/library/system.multicastdelegate.aspx
类委派事件
{
//委托也可以在类内定义
公共委托使另一个委托无效();
//委托可以像任何变量或字段一样实例化
公开另一个委托实例;
公共委托人事件()
{
//将方法/委托添加到调用列表
DelegateInstance+=方法;
//或者其他语法
DelegateInstance+=新的另一个委托(方法);
//删除调用列表中的方法/委托
DelegateInstance-=方法;
//还是更正式的语法
DelegateInstance-=新的另一个委托(方法);
//将调用列表设置为单个方法/委托