C#如何定义一个事件,实现事件的调用
4个回答
展开全部
public event EventHandler<EventArgs> MyEventToBeFired;
public void FireEvent(Guid instanceId, string handler)
{
// Note: this is being fired from a method with in the same class that defined the event (i.e. "this").
EventArgs e = new EventArgs(instanceId);
MulticastDelegate eventDelagate = (MulticastDelegate)this
.GetType()
.GetField(handler, BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(this);
Delegate[] delegates = eventDelagate.GetInvocationList();
foreach (Delegate dlg in delegates)
{
dlg.Method.Invoke( dlg.Target, new object[] { this, e } );
}
}
FireEvent(new Guid(), "MyEventToBeFired");
public void FireEvent(Guid instanceId, string handler)
{
// Note: this is being fired from a method with in the same class that defined the event (i.e. "this").
EventArgs e = new EventArgs(instanceId);
MulticastDelegate eventDelagate = (MulticastDelegate)this
.GetType()
.GetField(handler, BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(this);
Delegate[] delegates = eventDelagate.GetInvocationList();
foreach (Delegate dlg in delegates)
{
dlg.Method.Invoke( dlg.Target, new object[] { this, e } );
}
}
FireEvent(new Guid(), "MyEventToBeFired");
展开全部
using System;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
people p = new people("Name1");
p.PropertyChanged += new EventHandler(p_PropertyChanged);//注册事件处理函数
p.Name = "Name2";
Console.ReadKey();
}
static void p_PropertyChanged(object sender, EventArgs e)//事件的处理函数
{
Console.WriteLine("NamePropertyChanged:"+(sender as people).Name);
}
}
class people
{
public event EventHandler PropertyChanged;
private string name=String .Empty;
public people(string name)
{
this.name = name;
}
public string Name
{
get
set
{
name = value;
this.OnPropertyChanged(new EventArgs());//每次改变Name值调用方法;
}
}
private void OnPropertyChanged(EventArgs eventArgs)
{
if (this.PropertyChanged != null)//判断事件是否有处理函数
{
this.PropertyChanged(this, eventArgs);
}
}
}
}
//请参考
从你写的程序来看 从类的外部是不可能改变属性的值了,因为你的这两个属性都是只读的, 只能在类的内部改变_a _b的值
那样你写这个类的时候 你应该很清楚哪里改变了_a _b的值 真接调用相应的改变方法就行了
所以你写的这个类 要让属性在类外部也能改变其值 不然属性值的改变只在类内部并触发事件也就没什么意义了
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
people p = new people("Name1");
p.PropertyChanged += new EventHandler(p_PropertyChanged);//注册事件处理函数
p.Name = "Name2";
Console.ReadKey();
}
static void p_PropertyChanged(object sender, EventArgs e)//事件的处理函数
{
Console.WriteLine("NamePropertyChanged:"+(sender as people).Name);
}
}
class people
{
public event EventHandler PropertyChanged;
private string name=String .Empty;
public people(string name)
{
this.name = name;
}
public string Name
{
get
set
{
name = value;
this.OnPropertyChanged(new EventArgs());//每次改变Name值调用方法;
}
}
private void OnPropertyChanged(EventArgs eventArgs)
{
if (this.PropertyChanged != null)//判断事件是否有处理函数
{
this.PropertyChanged(this, eventArgs);
}
}
}
}
//请参考
从你写的程序来看 从类的外部是不可能改变属性的值了,因为你的这两个属性都是只读的, 只能在类的内部改变_a _b的值
那样你写这个类的时候 你应该很清楚哪里改变了_a _b的值 真接调用相应的改变方法就行了
所以你写的这个类 要让属性在类外部也能改变其值 不然属性值的改变只在类内部并触发事件也就没什么意义了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class EventTest
{
public delegate void MyEventHandler(int param);//定义委托
public event MyEventHandler MyEvent; //定义事件
public static void OnRaiseEvent(int param) //激发事件的函数
{
MyEvent(param);
}
}
调用时,首先安装事件
EventTest eventTest=new EventTest();
eventTest.MyEvent+=new MyEventHandler(MyFunction);//安装事件
public void MyFunction(int param)
{
Console.WriteLine(param);
}
{
public delegate void MyEventHandler(int param);//定义委托
public event MyEventHandler MyEvent; //定义事件
public static void OnRaiseEvent(int param) //激发事件的函数
{
MyEvent(param);
}
}
调用时,首先安装事件
EventTest eventTest=new EventTest();
eventTest.MyEvent+=new MyEventHandler(MyFunction);//安装事件
public void MyFunction(int param)
{
Console.WriteLine(param);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
双击所需要定义事件的控件就可以去写代码定义了,调用之前需要饮用.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询