我在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>
我似乎无法找到一个展示我想要做的事情的例子。我有什么想法可以让它发挥作用吗?
答案 0 :(得分: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);
}
答案 1 :(得分: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()