基本 GUI 事件处理问题 C#
下午好,
我有一些关于 GUI 事件处理的非常基本的问题。首先,使用 C# 我们如何将事件链接到对象 - 我猜是事件处理程序?如果是这样,每个处理程序可以使用单独的代码吗? - 事件处理程序如何定位它必须操作的对象?
我对JAVA的工作原理有一个大概的了解。给我一个参考就很好了——我已经在谷歌上搜索过答案,但没有结果。
非常感谢, J
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,每个事件处理程序都可以有自己的代码:
我将对此进行过度简化,但事件处理程序本质上是观察者模式的包装器。与任何其他委托类型一样,事件处理程序在方法调用列表中保存订阅者列表(请参阅 Delegate.GetInitationList)。您可以这样想:(
上面的代码与真实事件处理程序类的实际实现细节相去甚远。委托类型是不可变的,因此添加处理程序会返回一个添加了处理程序的新委托,而不是改变处理程序我相信他们的添加/删除方法也有很多线程巫术。)
由于委托实例保存了对其每个订阅者的引用,因此它可以直接访问它所操作的任何对象。
Yes, each event handler can have its own code:
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:
(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.
每个事件实际上都是一个委托 - 您可以使用以下表示法向该委托注册事件处理程序:
这样,当按钮单击事件触发时,其调用列表上的任何事件处理程序(在本例中将包括 myEventHandler< 对于每个按钮,您都执行相同的
操作并注册要触发的事件处理程序(多个按钮可以使用相同的事件处理程序)
:事件和代理。
Each event is in fact a delegate - you register the event handler with this delegate using this notation:
This way, when the button click event fires, any event handlers on its invocation list (in this case this will include the
myEventHandler
function.For each button, you do the same and register the event handler you want to fire (can be the same one for multiple buttons).
Perhaps this MSDN topic will shed some light: Events and Delegates.
使用 Visual Studio 中的设计器,您将可以轻松地使用 GUI 对象处理程序。通过单击“闪电”按钮,您将看到特定控件的所有可用事件。通过在选定的事件上单击两次,IDE 将为您生成一个方法存根。
事件处理程序像这样添加(在 *.Designer.cs 文件中自动完成)
Using designer in Visual Studio you will have an easy way with GUI objects handlers. By clicking the 'lightning' button you will see all available events for a partticular control. By clicking twice on selected event the IDE will generate a method stub for you.
The event handler is added like this(done automatically in the *.Designer.cs file)
这取决于您使用的 UI 库。在 WPF 中,您可以在 XAML 中定义事件处理程序:
在 WinForms 中,您可以将处理程序附加到表单的构造函数中:
当然,使用 Visual Studio 等 IDE 可以大大简化此过程。
有两种方法可以访问相关对象:
由于这些方法是窗口(WPF)或窗体(WinForms)的实例方法,因此它们可以直接访问所有 UI 资源(即,您可以使用
this. MyLabel
在代码中)。第一个参数
sender
按照惯例是引发事件的 UI 控件。如果您对多个 UI 控件使用一个事件处理程序,这非常有用。That depends on which UI library you are using. In WPF, you define the event handler in XAML:
In WinForms, you attach the handlers in the constructor of your form:
Of course, using a IDE such as Visual Studio simplifies this process a lot.
There are two ways to access the relevant objects:
Since the methods are instance methods of the window (WPF) or form (WinForms), they can access all the UI resources directly (i.e., you can use
this.MyLabel
in your code).The first parameter,
sender
, is by convention the UI control that raised the event. This is useful if you use one event handler for multiple UI controls.