基本GUI事件处理问题C# [英] Basic GUI Event Handling Questions C#
问题描述
下午好
我有GUI事件处理一些非常基本的问题。首先用C#怎么可以链接事件的对象 - 我猜事件处理程序?如果是的话可以在每个处理器使用不同的代码? - 事件处理程序如何才能找到它必须操作对象?
我它是如何工作的JAVA一个粗略的想法。指着我向参考就可以了 - 我已经拖网谷歌的答案无济于事。
非常感谢,
Ĵ
首先用C#怎样才能链路事件
的对象 - 我猜事件
处理程序?如果是这样就可以每个处理程序使用
单独的代码
块引用>
是的,每个事件处理程序都可以拥有自己的代码:
A级{
公共事件的EventHandler SomeEvent;
}
类B {
公众B(A一){
a.SomeEvent + =(发件人,E)=> {Console.WriteLine(B的处理程序); };
}
}
C类{
公共C(A中){
a.SomeEvent + =(发件人,E)=> {Console.WriteLine(C的处理程序); };
}
}
哪有事件处理器找到
对象必须操纵?
块引用>
我要简单化这个有很多,但事件处理程序本质上是周围的观察者模式包装。像任何其他委托类型事件处理器保持在一个方法调用列表订户名单(见委派。 GetInvocationList )。你可以认为它是这样的:
类事件处理程序{
LinkedList的<作用<对象,EventArgs的>>用户=
新的LinkedList<作用<对象,EventArgs的>>();
公共无效添加(动作<对象,EventArgs的> F){
subscribers.AddLast(F);
}
公共无效删除(动作<对象,EventArgs的> F){
subscribers.Remove(F);
}
公共无效调用(对象发件人,EventArgs五){
的foreach(动作<对象,EventArgs的> f由于用户)
F(发件人,E);
}
}
(上面的代码是很远从实际删除真实的事件处理程序类的实现细节。委托类型是不可改变的,因此增加一个处理程序返回的句柄新的代表补充,而不是突变到位的处理程序。我相信他们的添加/删除方法有他们很多线程巫术作为好。)
由于委托实例包含它的每个用户的一个参考,它有它操纵任何对象的直接访问。
Good Afternoon,
I have some very basic questions on GUI Event Handling. Firstly with C# how can we link events to objects - I am guessing event handlers? If so can each handler use separate code? - How can the event handler locate the objects it must manipulate?
I have a rough idea of how it works in JAVA. Pointing me towards a reference would be fine - I have already trawled Google for answers to no avail.
Many Thanks, J
解决方案Firstly with C# how can we link events to objects - I am guessing event handlers? If so can each handler use separate code?
Yes, each event handler can have its own code:
class A { public event EventHandler SomeEvent; } class B { public B(A a) { a.SomeEvent += (sender, e) => { Console.WriteLine("B's handler"); }; } } class C { public C(A a) { a.SomeEvent += (sender, e) => { Console.WriteLine("C's handler"); }; } }
How can the event handler locate the objects it must manipulate?
I'm going to oversimplify this a lot, but event handlers are essentially wrappers around the observer pattern. EventHandlers like any other Delegate type hold a list of subscribers in a method invocation list (see Delegate.GetInvocationList). You can think of it like this:
class EventHandler { LinkedList<Action<object, EventArgs>> subscribers = new LinkedList<Action<object, EventArgs>>(); public void Add(Action<object, EventArgs> f) { subscribers.AddLast(f); } public void Remove(Action<object, EventArgs> f) { subscribers.Remove(f); } public void Invoke(object sender, EventArgs e) { foreach(Action<object, EventArgs> f in subscribers) f(sender, e); } }
(The code above is pretty far removed from the actual implementation details of the real event handler class. Delegate types are immutable, so adding a handler returns a new Delegate with the handler added rather than mutating the handler in place. I believe their Add/Remove methods have a lot of threading voodoo in them as well.)
Since the delegate instance holds a reference to each of its subscribers, it has direct access to any object it manipulates.
这篇关于基本GUI事件处理问题C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!