帮我看看这点破事 EVENT
??????//免責(zé)聲明
本人想把自己對(duì)asp.net下委托和事件的簡(jiǎn)單理解,略舉了小例, 不知道這樣的委托事件,是否真的是破事。如果對(duì)事件的理解和用法錯(cuò)誤望高人斧正, 新手小弟莫要效尤,以免誤入歧途,走火入魔介紹
全局共4個(gè)文件:
??????Alarm.cs
????????????是個(gè)主題類,(好像是觀察者模式的一個(gè)名稱),想象成一個(gè)警報(bào)中心。它負(fù)責(zé)向CCTV,BBC等各大機(jī)構(gòu)提供服務(wù)
??????SafetyEventArgs
????????????主題向觀察者發(fā)送的信息參數(shù)類,包括防火指數(shù),防洪指數(shù)
??????ChannelCCTV1.aspx
????????????觀察者,向警報(bào)中心取得數(shù)據(jù),然后告訴各位看客。也就是三個(gè)按鈕??
???????ChannelCCTV1.aspx.cs
?????????????ChannelCCTV1.aspx的邏輯????
全局目錄結(jié)構(gòu)
??????????????????????????
?
1 先看 Alarm.cs
??????
?
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Alarm 的摘要說明
/// </summary>
public class Alarm
{
??? SafetyEventArgs sea;
??? public? delegate void DelegatePreAlarm(object sender, SafetyEventArgs e);
??? public event DelegatePreAlarm eventAlarm;
??? public void OnEvent()
??? {
??????? System.Random rnd = new Random();
??????? sea = new SafetyEventArgs();
??????? sea.FireIndex=rnd.Next(100);
??????? sea.FloodIndex=rnd.Next(100);
??????? if (this.eventAlarm != null)
??????? {
??????????? //第一個(gè)參數(shù)this將把類class Alarm傳出去
??????????? //傳遞給事件監(jiān)聽的所有方法
??????????? //所有方法有一個(gè)標(biāo)準(zhǔn),就是符合該事件相關(guān)的委托
??????????? //目前委托的規(guī)范是 輸出void? 輸入object, SafetyEventArgs;
??????????? //符合委托的規(guī)范的所有方法都可以交給事件監(jiān)聽,
??????????? //一旦這個(gè)方法(觸發(fā)事件的方法OnEvent)被調(diào)用,將發(fā)送給所有的觀察者
??????????? this.eventAlarm(this, sea);
??????? }
??? }
}
?
?
SafetyEventArgs.cs
?
?
?
?
Titleusing System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// SafetyEventArgs 的摘要說明
/// </summary>
public class SafetyEventArgs
{
??? /// <summary>
??? /// 防火指數(shù)
??? /// </summary>
??? private int fireIndex;
??? /// <summary>
??? /// Gets or sets the index of the fire.
??? /// </summary>
??? /// <value>The index of the fire.</value>
??? public int FireIndex
??? {
??????? get { return fireIndex; }
??????? set { fireIndex = value; }
??? }
??? /// <summary>
??? /// 防洪指數(shù)
??? /// </summary>
??? private int floodIndex;
??? /// <summary>
??? /// Gets or sets the index of the flood.
??? /// </summary>
??? /// <value>The index of the flood.</value>
??? public int FloodIndex
??? {
??????? get { return floodIndex; }
??????? set { floodIndex = value; }
??? }
??? /// <summary>
??? /// Initializes a new instance of the <see cref="SafetyEventArgs"/> class.
??? /// </summary>
?public SafetyEventArgs()
?{
?}
??? /// <summary>
??? /// Initializes a new instance of the <see cref="SafetyEventArgs"/> class.
??? /// </summary>
??? /// <param name="fire">The fire.</param>
??? /// <param name="flood">The flood.</param>
?public SafetyEventArgs(int fire,int flood)
?{
??????? this.fireIndex = fire;
??????? this.floodIndex = flood;
?}
?
??????
?
?
?
?
ChannelCCTV1.aspx
?
Title<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChannelCCTV1.aspx.cs" Inherits="ChannelCCTV1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>無標(biāo)題頁</title>
</head>
<body>
??? <form id="form1" runat="server">
??? <div>
??????? <asp:Button ID="Button1" runat="server" Text="訂閱綜合信息" OnClick="Button1_Click" />
??????? <asp:Button ID="Button2" runat="server" Text="訂閱防火信息" OnClick="Button2_Click"? />
??????? <asp:Button ID="Button3" runat="server" Text="訂閱防洪信息" OnClick="Button3_Click" />?
??? </div>
??? </form>
</body>
</html>
?
ChannelCCTV1.aspx.cs
?
Titleusing System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ChannelCCTV1 : System.Web.UI.Page
{
??? private string name = "CCTV1";
??? /// <summary>
??? /// 聲明主題
??? /// </summary>
??? private static Alarm alarm;
??? /// <summary>
??? /// 初始化主題
??? /// Handles the Load event of the Page control.
??? /// </summary>
??? /// <param name="sender">The source of the event.</param>
??? /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? alarm = new Alarm();
??? }
??? /// <summary>
??? /// 這是個(gè)符合委托規(guī)范的方法
??? /// 報(bào)道防火信息
??? /// Reports the fire.
??? /// </summary>
??? /// <param name="sender">The sender.</param>
??? /// <param name="e">The <see cref="SafetyEventArgs"/> instance containing the event data.</param>
??? public void ReportFire(object sender, SafetyEventArgs e)
??? {
??????? string resp = string.Format("<br>sender is:" + sender.ToString() + "<br />" + " info: fire index is:{0}" + "<br />" , e.FireIndex);
??????? Response.Write(resp);
??????? Response.Write(" by "+this.name);
??? }
??? /// <summary>
??? /// 這是個(gè)符合委托規(guī)范的方法
??? /// 報(bào)道防洪信息
??? /// Reports the flood.
??? /// </summary>
??? /// <param name="sender">The sender.</param>
??? /// <param name="e">The <see cref="SafetyEventArgs"/> instance containing the event data.</param>
??? public void ReportFlood(object sender, SafetyEventArgs e)
??? {
??????? string resp = string.Format("<br>sender is:" + sender.ToString() + "<br />" + " info: flood index is:{0}",e.FloodIndex);
??????? Response.Write(resp);
??????? Response.Write("<br> by "+this.name);
??? }
??? protected void Button1_Click(object sender, EventArgs e)
??? {
??????? alarm.eventAlarm += new Alarm.DelegatePreAlarm(this.ReportFire);
??????? alarm.eventAlarm += new Alarm.DelegatePreAlarm(this.ReportFlood);
??????? alarm.OnEvent();
??? }
??? protected void Button2_Click(object sender, EventArgs e)
??? {
??????? alarm.eventAlarm += new Alarm.DelegatePreAlarm(this.ReportFire);
??????? alarm.OnEvent();
??? }
??? protected void Button3_Click(object sender, EventArgs e)
??? {
??????? alarm.eventAlarm += new Alarm.DelegatePreAlarm(this.ReportFlood);
??????? alarm.OnEvent();
??? }
}
?
?
?
?
?
運(yùn)行效果
?
?
???????????????????????????????
?
?
訂閱綜合效果
?
?
?
?
訂閱防火信息效果
?
?
over,看大家的笑果
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/techPioneer/archive/2009/05/10/1453650.html
總結(jié)
以上是生活随笔為你收集整理的帮我看看这点破事 EVENT的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET MVC雕虫小技 1-2
- 下一篇: 定义系统消息 Specify syste