我正在尝试使用新的(ish)AS3全局错误处理类。我正在尝试在 Flex mxml 应用程序中使用它。我根本无法让它工作。下面是显示问题的完整程序。我将其设置为使用 Flash Player 10.2 并使用 Flex 4.5 SDK 进行编译。
我尝试使用 Flex SDK 4.0 和 4.5,但无论哪种情况都出现错误。我一定在这里错过了一些明显的东西。这是一个普通的 Flex SWF 文件,将显示在网页上。假设我可以导入 UncaughtErrorEvent,然后我会做这样的事情来设置事件处理程序:
if(systemManager.loaderInfo.hasOwnProperty("uncaughtErrorEvents")) {
IEventDispatcher(
systemManager.loaderInfo["uncaughtErrorEvents"]).addEventListener(
"uncaughtError", uncaughtErrorHandler);
}
这一切看起来都非常笨拙,但我可以忍受,除了它不起作用!我已经搜索了网络,找不到任何文档或示例来解释如何在我的上下文中使其工作。有什么建议吗?
完整程序:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="onApplicationComplete();"
>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function onApplicationComplete() : void {
systemManager.loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", uncaughtErrorHandler);
}
private function uncaughtErrorHandler(event:Event) : void {
trace(event.toString());
}
]]>
</fx:Script>
<s:Button x="153" y="64" label="Trigger Error" id="triggerButton" click="throw new Error('myError')"/>
</s:Application>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
uncaughtError="uncaughtErrorHandler(event)" />
简单的方法...
从 API 中获取(我建议您下次查看):
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
applicationComplete="applicationCompleteHandler();">
<fx:Script>
<![CDATA[
import flash.events.ErrorEvent;
import flash.events.MouseEvent;
import flash.events.UncaughtErrorEvent;
private function applicationCompleteHandler():void
{
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
if (event.error is Error)
{
var error:Error = event.error as Error;
// do something with the error
}
else if (event.error is ErrorEvent)
{
var errorEvent:ErrorEvent = event.error as ErrorEvent;
// do something with the error
}
else
{
// a non-Error, non-ErrorEvent type was thrown and uncaught
}
}
private function clickHandler(event:MouseEvent):void
{
throw new Error("Gak!");
}
]]>
</fx:Script>
<s:Button label="Cause Error" click="clickHandler(event);"/>
</s:WindowedApplication>