由ActionScript3入Silverlight之事件篇 |
| [来源:圣叹 | 作者:圣叹 | 时间:2008-05-25 | 点击: | 收藏本文 【大 中 小】] |
|
最近在学C#,微软的IDE做的还是极其强悍的。对比ActionScript3学C#很有意思的,万一哪天Microsoft Silverlight真打败了Adobe Flash,有C#还能继续混口饭吃。想起猫和老鼠的故事:“看,这就是多一门语言的妙处”,笑。 事件(event)是Observer设计模式的最佳体现,不论是ActionScript还是C#,事件在其中占据重要位置,尤其是在窗口应用程序中。熟悉ActionScript3的都应该知道几乎所有事件都继承自Event这个类。自定义一个事件也不例外。而在C#中,事件通常是EventArgs的子类:
- public class ActionCancelEventArgs : EventArgs
- {
- string message;
-
- public ActionCancelEventArgs() { }
- public ActionCancelEventArgs(string message)
- {
- this.message = message;
- }
- public string Message
- {
- get { return message; }
- set { message = value; }
- }
- }
所不同的是,在类中自定义事件是有区别的。C#中不可以直接将方法作为参数传递,此时需要用到委托(delegate)这一概念。委托是一个特别的类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。C#使用委托主要是出于类型安全的考虑。注意这里定义的委托的参数,前者是一个对象(其实这里传递的是对象的引用,如果是button1的click事件则sender就是button1),表名触发折;后面是包含事件数据的类的基类。比如下面的示例:
- public class ActionSample
- {
-
- public delegate void ActionEventHandler(object sender, ActionCancelEventArgs eventArgs);
-
- public event ActionEventHandler Action;
-
-
- protected void OnAction(object sender, ActionCancelEventArgs eventArgs)
- {
- if (Action != null)
- Action(sender, eventArgs);
- }
-
-
- public void dispatch()
- {
- ActionCancelEventArgs ev = new ActionCancelEventArgs("An Event was dispatched.");
- OnAction(this, ev);
- }
- }
因此,在C#中,声明一个事件类似于声明一个进行了封装的委托类型的变量。似乎由此看来,C#在事件的声明这部分变得异常麻烦。但是带来的结果是添加事件的监听则变得非常简单。如:
- ActionSample actionExample = new ActionSample();
- actionExample.Action += onAction;
- actionExample.dispatch();
毕竟,相比较广播事件,而监听事件更为常用。onAction即监听函数:
- private void onAction(object sender, ActionSample.ActionCancelEventArgs eventArgs)
- {
-
- }
同为Observer设计模式,ActionScript3中的事件则不同。看下面类似的代码:
- class ActionCancelEventArgs extends Event
- {
- public static const ON_ACTION:String = "onAction";
- public var message:String;
-
- public function ActionCancelEventArgs( message:String )
- {
- this.message = message;
- super( ON_ACTION );
- }
- }
ActionCancelEventArgs事件多了一个参数表名事件类型,这个参数通常是静态常量。与C#的委托相比,这使得监听的过程变得很简单:
- package {
- import flash.display.Sprite;
- import flash.events.Event;
-
- public class Action extends Sprite
- {
- public function Action()
- {
- var actionExample:ActionSample = new ActionSample();
-
- actionExample.addEventListener( ActionCancelEventArgs.ON_ACTION, this.onAction );
- }
-
- public function onAction( event:Event ):void
- {
-
- }
- }
- }
- import flash.events.Event;
- import flash.display.DisplayObject;
-
- class ActionSample extends DisplayObject
- {
- public function dispatch():void
- {
- this.dispatchEvent( new ActionCancelEventArgs( "message" ) );
- }
- }
-
- class ActionCancelEventArgs extends Event
- {
- public static const ON_ACTION:String = "onAction";
- public var message:String;
-
- public function ActionCancelEventArgs( message:String )
- {
- this.message = message;
- super( ON_ACTION );
- }
- }
C#实例代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
-
- }
-
- public class ActionSample
- {
- public delegate void ActionEventHandler(object sender, ActionCancelEventArgs eventArgs);
- public event ActionEventHandler Action;
-
- protected void OnAction(object sender, ActionCancelEventArgs eventArgs)
- {
- if (Action != null)
- Action(sender, eventArgs);
- }
-
- public void dispatch()
- {
- ActionCancelEventArgs ev = new ActionCancelEventArgs("An Event was dispatched.");
- OnAction(this, ev);
- }
-
- public class ActionCancelEventArgs : EventArgs
- {
- string message;
-
- public ActionCancelEventArgs() { }
- public ActionCancelEventArgs(string message)
- {
- this.message = message;
- }
- public string Message
- {
- get { return message; }
- set { message = value; }
- }
- }
- }
-
- private void button_Click(object sender, EventArgs e)
- {
- ActionSample actionExample = new ActionSample();
- actionExample.Action += onAction;
-
- actionExample.dispatch();
- }
-
- private void onAction(object sender, ActionSample.ActionCancelEventArgs eventArgs)
- {
- this.textBox.Text="On Action";
- }
-
- }
- }
|
|
| 文章如果有错误或者缺少文件,请发邮件提交给我们 |
|
|
|
|
|
|
|
| >>> 最新评论:(共有 0 位网友发表了评论) 查看所有评论 |
|
|
| 发表评论 |
|
| ·本站发布内容均为客观表达作者观点,不代表闪无忧立场,请勿攻击和漫骂 |
| ·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任 |
| ·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据 |
| ·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为 |
|
|
|
 |
教程分类 |
|
|
|
|
|
|