仍然无法理解 ASP.NET 事件.他们有什么意义? [英] Still having problems understanding ASP.NET events. What's the point of them?

查看:28
本文介绍了仍然无法理解 ASP.NET 事件.他们有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我很慢,但我只是不明白为什么您会使用并非源自实际操作(如点击)的事件.当您可以调用一个方法时,为什么还要经历创建委托和事件的繁琐呢?似乎当您创建一个事件时,您所做的只是为调用者创建一种方法,让调用者通过一些复杂的过程来调用一个简单的方法.调用者必须自己引发事件!没看懂.

Maybe I'm slow, but I just don't get why you would ever use an event that is not derived from an actual action (like clicking). Why go through the rigamarole of creating delegates and events when you can just call a method? It seems like when you create an event, all you're doing is creating a way for the caller to go through some complicated process to call a simple method. And the caller has to raise the event themselves! I don't get it.

或者我只是没有理解这个概念.我需要像 OnClick 这样的事件以及与控件的交互,但是对于类呢?我尝试为我的一个类实现事件,例如,当项目的来源发生变化时,但很快意识到没有意义,因为我可以在想要执行某个操作而不是创建事件时调用一个方法,引发事件,并编写事件处理程序.另外,我可以重用我的方法,但我不一定能重用我的事件处理程序.

Or maybe I'm just not grasping the concept. I get the need for events like OnClick and interactions with controls, but what about for classes? I tried to implement events for a class of mine, say, when the source of an item changed, but quickly realized that there was no point since I could just call a method whenever I wanted to perform a certain action instead of creating an event, raising an event, and writing an event handler. Plus, I can reuse my method, whereas I can't necessarily reuse my event handler.

请有人纠正我.我觉得我在这里错了,我想得到纠正.我问的最后一个问题并没有真正得到任何有用的答案.

Someone set me straight please. I feel like I'm just wrong here, and I want to be corrected. The last question I asked didn't really garner any sort of helpful answer.

谢谢.

推荐答案

我一直喜欢广播电台的比喻.

I've always like the Radio Station metaphor.

当广播电台想要广播某些内容时,它只是将其发送出去.它不需要知道是否真的有人在听.您的收音机能够向广播电台注册(通过拨盘调谐),所有广播电台的广播(我们的小比喻中的事件)都被收音机接收,并将它们翻译成声音.

When a radio station wants to broadcast something, it just sends it out. It doesn't need to know if there is actually anybody out there listening. Your radio is able to register itself with the radio station (by tuning in with the dial), and all radio station broadcasts (events in our little metaphor) are received by the radio who translates them into sound.

没有这种注册(或事件)机制.广播电台必须轮流联系每一个电台,询问它是否想要广播,如果你的电台说是,然后直接向它发送信号.

Without this registration (or event) mechanism. The radio station would have to contact each and every radio in turn and ask if it wanted the broadcast, if your radio said yes, then send the signal to it directly.

您的代码可能遵循非常相似的范例,其中一个类执行一项操作,但该类可能不知道,或者可能不想知道谁会关心或对发生的该操作采取行动.因此,它为任何对象提供了一种注册或注销自身的方法,以通知该操作已发生.

Your code may follow a very similar paradigm, where one class performs an action, but that class may not know, or may not want to know who will care about, or act on that action taking place. So it provides a way for any object to register or unregister itself for notification that the action has taken place.

这篇关于仍然无法理解 ASP.NET 事件.他们有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆