zoukankan      html  css  js  c++  java
  • delegate(委托)、event(事件) 的基础知识

    1. event和delegate类型的字段到底有什么区别

    http://www.cnblogs.com/Microshaoft/archive/2005/05/30/164753.html

    2. 事件与事件处理程序

    a)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;

    namespace WebApplication14
    {
    public partial class _Default : System.Web.UI.Page
    {
    private static StringBuilder mysb = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
    HasEvent he
    = new HasEvent();

    he.SampleEvent
    += new HasEvent.MyEventHandler(SampleEventHandler);

    he.DemoEvent(
    "Hey there, Bruce!");
    he.DemoEvent(
    "How are you today?");
    he.DemoEvent(
    "I'm pretty good.");
    he.DemoEvent(
    "Thanks for asking!");
    Label1.Text
    = mysb.ToString() ;
    }

    private static void SampleEventHandler(object src, MyEventArgs mea)
    {
    mysb.Append(mea.Message);
    }
    }

    public class MyEventArgs : EventArgs
    {
    private string msg;

    public MyEventArgs(string messageData)
    {
    msg
    = messageData;
    }
    public string Message
    {
    get { return msg; }
    set { msg = value; }
    }
    }
    //---------------------------------------------------------
    public class HasEvent
    {
    // Declare an event of delegate type MyEventHandler of
    // MyEventArgs.


    public delegate void MyEventHandler(object sender, MyEventArgs e);
    public event MyEventHandler SampleEvent;

    public void DemoEvent(string val)
    {
    // Copy to a temporary variable to be thread-safe.

    MyEventHandler temp
    = SampleEvent;
    if (temp != null)
    temp(
    this, new MyEventArgs(val));
    }
    }
    }

    b)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;

    namespace WebApplication14
    {
    public partial class _Default : System.Web.UI.Page
    {
    private static StringBuilder mysb = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
    HasEvent he
    = new HasEvent();
    he.SampleEvent
    +=
    new EventHandler<MyEventArgs>(SampleEventHandler);

    he.DemoEvent(
    "Hey there, Bruce!");
    he.DemoEvent(
    "How are you today?");
    he.DemoEvent(
    "I'm pretty good.");
    he.DemoEvent(
    "Thanks for asking!");
    Label1.Text
    = mysb.ToString() ;
    }

    private static void SampleEventHandler(object src, MyEventArgs mea)
    {
    mysb.Append(mea.Message);
    }
    }

    public class MyEventArgs : EventArgs
    {
    private string msg;

    public MyEventArgs(string messageData)
    {
    msg
    = messageData;
    }
    public string Message
    {
    get { return msg; }
    set { msg = value; }
    }
    }
    //---------------------------------------------------------
    public class HasEvent
    {
    // Declare an event of delegate type EventHandler of
    // MyEventArgs.

    public event EventHandler<MyEventArgs> SampleEvent;

    public void DemoEvent(string val)
    {
    // Copy to a temporary variable to be thread-safe.
    EventHandler<MyEventArgs> temp = SampleEvent;
    if (temp != null)
    temp(
    this, new MyEventArgs(val));
    }
    }
    }

    c) 假定用户控件(UserControl.ascx)中包含按钮控件Save,希望实现按Save按钮时,包含该用户控件的页面(Default.aspx)可以接收到事件。


    UserControl.ascx.cs

    public event EventHandler SaveClick;

    protected void Save_Click(object sender, EventArgs e)
    {
    if (SaveClick != null)
    SaveClick(
    this, e);
    }

    Default.aspx.cs 代码

    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" OnSaveClick="Save_Click" />

    protected void Save_Click(object sender, EventArgs e)
    {
    }

  • 相关阅读:
    这样的专家门诊
    CPU 知识介绍
    软考结束了!
    ubuntu环境配置eclipse+opencv
    2016年3月份总结
    点聚合功能基于ARCGIS RUNTIME SDK FOR ANDROID
    如何用java语言获取某个网页的源代码
    点聚合功能改良版本
    ARCGIS切图:TPK文件的空间参考为地理坐标系
    Java中的数据类型
  • 原文地址:https://www.cnblogs.com/zhuwenlubin/p/1897872.html
Copyright © 2011-2022 走看看