引发由 .Net Web 应用程序的完全独立部分处理的控制事件

发布于 2024-12-06 11:32:13 字数 493 浏览 1 评论 0原文

各位,

我感觉这个案例有一个经典的设计模式,但我手头没有参考资料。

我遇到一种情况,用户单击按钮,网页的另一部分做出响应。不寻常(或至少不方便)的是,按钮和网页的响应部分处于非常不同的控制层次结构中,无论是在与共同祖先的距离方面还是在应用程序关注点方面。它们都是一系列(独立的)黑匣子的内部部分。

我无法想出一种优雅的方式来触发另一个。我不想将一系列事件提升到控件层次结构的顶部,然后调用一系列嵌套方法来实现所需的效果。 (“effect”!=视觉效果;我无法用 jQuery 解决这个问题)我希望事件和响应者保持彼此不知情:我不会因为让后者了解并直接订阅而违反关注点分离前者产生的事件。我考虑过许多​​其他不优雅的解决方案,但我不会列出它们来让您感到厌烦。

基本上我想我需要一种方法来实现事件的发布者-订阅者模型,其中任何东西都可以订阅任何东西。

我正在使用.Net 4.0。有什么好的框架可以做到这一点吗?有什么简单的方法可以实现吗?

非常感谢,

安·L.

Folks,

I have a feeling there's a classic design pattern that covers this case, but I don't have a reference handy.

I have a situation where a user clicks a button and another part of the web page responds. What's unusual (or at least inconvenient) is that the button and the reponding part of the web page are in very different control hierarchies, both in terms of distance to common ancestor and in terms of application concern. They're both inner parts of a series of (separate) black boxes.

I have not been able to come up with an elegant way for the one to trigger the other. I do not want to have to raise a series of events up to the top of the control hierarchy and then call a series of nested methods to make the desired effect happen. ("effect" != visual effect; I can't solve this with jQuery) I want the event and the responder to remain ignorant of each other: I will not violate separation of concerns by having the latter know about and directly subscribe to an event generated by the former. I've considered a number of other inelegant solutions, but I won't bore you by listing them off.

Basically I guess I need a way to implement a publisher-subscriber model for events in which anything can subscribe to anything.

I'm using .Net 4.0. Any nice frameworks that do this? Any easy ways to implement it?

Thanks a lot,

Ann L.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执妄 2024-12-13 11:32:13

听起来您需要一个 EventAggregator。您应该在复合应用程序库中找到该实现。

EventAggregator 允许您注册通知并从应用程序的完全独立的部分引发它们。

Sounds like you need an EventAggregator. You should find the implementation in the Composite Application Library.

The EventAggregator allows you to register to notifications and raise them from completely separated parts of the application.

吝吻 2024-12-13 11:32:13

对于服务器端事件,您可以使用:
反应式扩展 (Rx)...
http://msdn.microsoft.com/en-us/data/gg577609

对于客户端事件,您可以使用:
骨干JS
http://documentcloud.github.com/backbone/
淘汰赛JS
http://knockoutjs.com/

你提到你不能使用 jQuery 我可以问为什么吗?

For server side events you can use:
The Reactive Extensions (Rx)...
http://msdn.microsoft.com/en-us/data/gg577609

For Client side events you can use:
Backbone JS
http://documentcloud.github.com/backbone/
Knockout JS
http://knockoutjs.com/

You mentioned you couldn't use jQuery may I ask why?

缱绻入梦 2024-12-13 11:32:13

我不确定我是否理解正确,但听起来您想将多个按钮分配给同一个事件处理程序,您可以这样做:

<asp:Button ID="Button1" runat="server" Text="Button 1" OnClick="Button_Click" ... />
<asp:Button ID="Button2" runat="server" Text="Button 2" OnClick="Button_Click" ... />

在隐藏代码中:

protected void Button_Click(object sender, EventArgs e)
{
    if (sender.Equals(Button1))
    {
        //Button1 logic
    }
    else if (sender.Equals(Button2))
    {
        //Button2 logic
    }
}

编辑

基于您的评论,听起来您可能需要一个事件处理程序。如果是这样,请尝试这样的事情:

public event EventHandler ButtonClick;

protected void Button_Click(object sender, EventArgs e)
{
    if (this.ButtonClick != null)
        this.ButtonClick(sender, e);
}    

I'm not sure if I understand you correctly, but it sounds like you want to assign multiple buttons to the same event handler, which you can do like this:

<asp:Button ID="Button1" runat="server" Text="Button 1" OnClick="Button_Click" ... />
<asp:Button ID="Button2" runat="server" Text="Button 2" OnClick="Button_Click" ... />

In the code-behind:

protected void Button_Click(object sender, EventArgs e)
{
    if (sender.Equals(Button1))
    {
        //Button1 logic
    }
    else if (sender.Equals(Button2))
    {
        //Button2 logic
    }
}

EDIT

Based on your comment, it sounds like you might need an event handler. If so, try something like this:

public event EventHandler ButtonClick;

protected void Button_Click(object sender, EventArgs e)
{
    if (this.ButtonClick != null)
        this.ButtonClick(sender, e);
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文