星空网 > 软件开发 > ASP.net

事件的标准模式

摘自http://www.cnblogs.com/willick/p/4172174.html,仅供参考学习

.NET框架为事件编程定义了一个标准模式,设定这个标准是为了让.NET框架和用户代码保持一致。System.EventArgs是标准模式的核心,它是一个没有任何成员,用于传递事件参数的基类,首先定义EventArgs:

1 public class PriceChangeEventArgs : System.EventArgs {2     public readonly decimal oldPrice;3     public readonly decimal newPrice;4     public PriceChangeEventArgs(decimal oldPrice, decimal newPrice) {5 6       this.oldPrice = oldPrice;7       this.newPrice = newPrice;8     }9   }

然后为事件定义委托,必须满足以下条件:

  • 必须是 void 返回类型;
  • 必须有两个参数,且第一个是object类型,第二个是EventArgs类型(的子类);
  • 它的名称必须以EventHandler结尾。

由于考虑到每个事件都要定义自己的委托很麻烦,.NET 框架为我们预定义好一个通用委托System.EventHandler<TEventArgs>:

public delegate void EventHandler<TEventArgs> (object source, TEventArgs e) where TEventArgs : EventArgs;

如果不使用框架的EventHandler<TEventArgs>,我们需要自己定义一个:

public delegate void PriceChangedEventHandler (object sender, PriceChangedEventArgs e);

如果不需要参数,可以直接使用EventHandler(不需要<TEventArgs>)。有了EventHandler<TEventArgs>,我们就可以这样定义示例中的事件:

public class IPhone6 {  ...  public event EventHandler<PriceChangedEventArgs> PriceChanged;  ...}

最后,事件标准模式还需要写一个受保护的虚方法来触发事件,这个方法必须以On为前缀,加上事件名(PriceChanged),还要接受一个EventArgs参数,如下:

1 public class IPhone6 {2   ...3   public event EventHandler<PriceChangedEventArgs> PriceChanged;4   protected virtual void OnPriceChanged(PriceChangedEventArgs e) {5     if (PriceChanged != null) PriceChanged(this, e);6   }7   ...8 }

下面给出完整示例:

public class PriceChangedEventArgs : System.EventArgs {  public readonly decimal OldPrice;  public readonly decimal NewPrice;  public PriceChangedEventArgs(decimal oldPrice, decimal newPrice) {    OldPrice = oldPrice;    NewPrice = newPrice;  }}public class IPhone6 {  decimal price;  public event EventHandler<PriceChangedEventArgs> PriceChanged;  protected virtual void OnPriceChanged(PriceChangedEventArgs e) {    if (PriceChanged != null) PriceChanged(this, e);  }  public decimal Price {    get { return price; }    set {      if (price == value) return;      decimal oldPrice = price;      price = value;      // 如果调用列表不为空,则触发。      if (PriceChanged != null)        OnPriceChanged(new PriceChangedEventArgs(oldPrice, price));    }  }}class Program {  static void Main() {    IPhone6 iphone6 = new IPhone6() { Price = 5288M };    // 订阅事件    iphone6.PriceChanged +=iphone6_PriceChanged;    // 调整价格(事件发生)    iphone6.Price = 3999;    Console.ReadKey();  }  static void iphone6_PriceChanged(object sender, PriceChangedEventArgs e) {    Console.WriteLine("年终大促销,iPhone 6 只卖 " + e.NewPrice + " 元, 原价 " + e.OldPrice + " 元,快来抢!");  }}




原标题:事件的标准模式

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊欧洲VAT税率政策详解:https://www.kjdsnews.com/a/1353055.html
什么是澳大利亚纽扣电池认证标准?:https://www.kjdsnews.com/a/1353056.html
数据 | WIPO发布2022年知识产权之“最”:https://www.kjdsnews.com/a/1353057.html
0评论是如何涌进best seller的?深挖其中奥秘!:https://www.kjdsnews.com/a/1353058.html
法国市场需求井喷,这个平台逆势增长不容小觑:https://www.kjdsnews.com/a/1353059.html
2023跨境品牌海外网红营销白皮书:洞察4大变化与趋势!:https://www.kjdsnews.com/a/1353060.html
秦皇岛到丹东旅游景点大全 秦皇岛到丹东开车多长时间:https://www.vstour.cn/a/408253.html
求推荐适合情侣玩的地方,三天时间,谢谢:https://www.vstour.cn/a/408254.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流