[英]Custom Flash/Flex Event object
我在Flex Mobile應用程序項目中。 我需要將事件調度到FlexGlobals.topLevelApplication,它必須包含自定義消息。
我正在嘗試制作一個對象並像這樣調度它:
//create the event Object
var receivedObjMsg:Object = new Object();
receivedObjMsg.name = "receivedMessage";
receivedObjMsg.message = messagevarhere;
FlexGlobals.topLevelApplication.dispatchEvent(receivedObjMsg);
然后像這樣在另一個視圖上接收它:
FlexGlobals.topLevelApplication.addEventListener("receivedMessage", receiveMsgHandler);
protected function receiveMsgHandler(event:Event):void
{
trace("IT WORKED!");
}
但是它的說法不能使對象成為事件:
Type Coercion failed: cannot convert Object@5a507911 to flash.events.Event.
我還嘗試將其放在創建事件的主應用程序mxml的底部。
<fx:Metadata>
[Event(name="receivedMessage", type="flash.events.Event")]
</fx:Metadata>
我似乎似乎找不到一個可以說明我正在嘗試做的事的例子。 有什么想法可以使它起作用嗎?
dispatchEvent
接受一個Event
創建您自己的擴展Event
的類,然后調度它。
看一下這篇文章 ,討論如何調度自定義事件。
class MyOwnEvent extends Event
{
public static const RECEIVED_EVENT:String = "receivedEvent";
public string name;
public string message;
public MyOwnEvent (type:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
}
}
而當你想調度它。
var myevent:MyOwnEvent = new MyOwnEvent(MyOwnEvent.RECEIVED_EVENT);
myevent.name = "whatever";
myevent.message = "another whatever";
FlexGlobals.topLevelApplication.dispatchEvent(myevent);
確保從topLevelApplication,您偵聽相同的事件。
FlexGlobals.topLevelApplication.addEventListener(MyOwnEvent.RECEIVED_EVENT, receiveMsgHandler);
在receiveMsgHandler
,使用MyOwnEvent
類型的對象。
protected function receiveMsgHandler(event:MyOwnEvent):void
{
trace(event.name);
trace(event.message);
}
dispachEvent()
僅接受一個Event
對象。 您需要創建自己的類ReceivedObjMsg
。
有關上一個問題的答案中創建自己的班級的詳細信息。
您的問題基本上在這里:
var receivedObjMsg:Object = new Object();
receivedObjMsg.name = "receivedMessage";
receivedObjMsg.message = messagevarhere;
FlexGlobals.topLevelApplication.dispatchEvent(receivedObjMsg);
通過dispatchEvent()
解析Object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.