有关代码隐藏的问题
主文件test002.mxml的内容:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<cookbook:CodeBehindComponent xmlns:mx="http://www.adobe.com/2006/mxml" width="200" height="400" xmlns:cookbook="cookbook.*">
<mx:Button click="clickHandler(event)" />
</cookbook:CodeBehindComponent>
</mx:Application>
在和主文件同一目录下有一文件夹cookbook,在cookbook里面有一文件CodeBehindComponent.as,其内容如下:
package cookbook
{
import mx.containers.Canvas;
import flash.events.Event;
public class CodeBehindComponent extends Canvas
{
public function CodeBehindComponent()
{
super();
addEventListener(Event.ADDED_TO_STAGE,addedToStageListener);
}
protected function addedToStageListener(event:Event):void
{
trace("Added to Stage from CodeBehind");
}
public function clickHandler(event:Event):void
{
trace("Click handled from component"+event.target);
}
}
}
现在报错:
Severity and Description Path Resource Location Creation Time Id
1180: 调用的方法 clickHandler 可能未定义。 Test002/src Test002.mxml line 4 1268029681609 73
这是书上的例子啊。如果将这一行注释掉,便不再报错了。这说明目录结构还是正确的。但为什么就说clickHandler没定义呢?
请大家帮忙啊!
[解决办法]
尝试:
<mx:Button click="cookbook.CodeBehindComponent.clickHandler(event)" />
[解决办法]
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<cookbook:CodeBehindComponent xmlns:mx="http://www.adobe.com/2006/mxml"
width="200"
height="400"
id="aa" xmlns:cookbook="cookbook.*">
<mx:Button click="aa.clickHandler(event)"/>
</cookbook:CodeBehindComponent>
</mx:Application>