2011-06-23 54 views
0

我在Flex Mobile应用程序项目中。我需要将事件分派给FlexGlobals.topLevelApplication,并且它必须包含一条自定义消息。自定义Flash/Flex事件对象

我试图让一个对象,并派遣这样说:

//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 [email protected] to flash.events.Event. 

我也试图把这个主APPLICAT底部离子mxml在哪里我创建的事件;

<fx:Metadata> 
[Event(name="receivedMessage", type="flash.events.Event")] 
</fx:Metadata> 

我真的不能找到一个示例来说明我在做什么。任何想法如何让这个工作?

回答

0

dispachEvent()只接受Event对象。你需要制作自己的课ReceivedObjMsg

有关在previous question of yours的答案中创建自己的班级的详细信息。

您的问题基本上是在这里:

var receivedObjMsg:Object = new Object(); 
receivedObjMsg.name = "receivedMessage"; 
receivedObjMsg.message = messagevarhere; 

FlexGlobals.topLevelApplication.dispatchEvent(receivedObjMsg); 

解析Object通过dispatchEvent()

+0

我知道我看到它在某个地方,但我不记得我在哪里看过它,对不起! – brybam

1

dispatchEvent需要一个Event

创建自己的类,它扩展Event,然后派遣这一点。

看看this article,它讨论了如何分派自定义事件。

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); 
}