如何处理 AS3 中启用 HTML 的文本字段中的 IOErrors

发布于 2024-08-22 01:01:56 字数 360 浏览 10 评论 0 原文

如果我通过 加载图像动态文本字段中的标签并引发 IOError,我还要附加什么事件侦听器?文本字段? 我尝试过这个...

var textField:TextField = new TextField();
textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
textField.addEventListener(IOErrorEvent.IOError, function (e:Event):void { trace("error caught") });

无济于事...

建议?

If I'm loading images via the <img> tag in a dynamic text field and an IOError is thrown, what would I attach the event listener too? the text field?
I tried this...

var textField:TextField = new TextField();
textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
textField.addEventListener(IOErrorEvent.IOError, function (e:Event):void { trace("error caught") });

to no avail...

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身骄傲 2024-08-29 01:01:56

您必须将 id 设置为 img,然后在 getImageReference 在您的 TextField 上获取 Loader,您可以在其中添加所有 您想要的事件

import flash.display.Loader;
import flash.events.IOErrorEvent;
import flash.text.TextField;

//...
var tfd:TextField = new TextField();
tfd.htmlText = 
      "here is some text <img id='myImg' src='image.jpg' /> and then some more";
var ldr:Loader = tfd.getImageReference("myImg") as Loader;
if (ldr != null) {
 ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
}
//...
private function onIOError(e:IOErrorEvent):void{
 //...
}

另一个示例 此处

You have to set an id to img and then use it within getImageReference on your TextField to get the Loader where you can add all the Event you want:

import flash.display.Loader;
import flash.events.IOErrorEvent;
import flash.text.TextField;

//...
var tfd:TextField = new TextField();
tfd.htmlText = 
      "here is some text <img id='myImg' src='image.jpg' /> and then some more";
var ldr:Loader = tfd.getImageReference("myImg") as Loader;
if (ldr != null) {
 ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
}
//...
private function onIOError(e:IOErrorEvent):void{
 //...
}

Another example here if you want

听风吹 2024-08-29 01:01:56

我为你提供了解决方案:

tField.addEventListener( Event.ADDED, addedObjectToFieldHandler, true );

function addedObjectToFieldHandler( event:Event ):void
{
   if ( event.target is Loader )
   {
       ( event.target as Loader ).contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function( e:IOErrorEvent ):void{} );
   }
}

这将防止 flashplayer 在图像链接损坏时抛出错误并崩溃

i've solution for ya:

tField.addEventListener( Event.ADDED, addedObjectToFieldHandler, true );

function addedObjectToFieldHandler( event:Event ):void
{
   if ( event.target is Loader )
   {
       ( event.target as Loader ).contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, function( e:IOErrorEvent ):void{} );
   }
}

that will prevent flashplayer from throwing errors and crash whenever image link is broken

永言不败 2024-08-29 01:01:56

你必须使用 try catch 块:

try
{
  var textField:TextField = new TextField();
  textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
}
catch( error:IOError )
{
    //handle IOError
}

you have to use a try catch block:

try
{
  var textField:TextField = new TextField();
  textField.htmlText = "here is some text <img src='image.jpg'> and then some more";
}
catch( error:IOError )
{
    //handle IOError
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文