这是在 mxml 中调用组件方法的正确方法吗
我正在通过学习 Flex 的方式进行破解,并发现了一些奇怪的行为。当我尝试编译代码时,出现此错误 - 错误:调用可能未定义的方法 updateStory。我以前以这种方式使用过方法调用,但无法发现这种情况下出了什么问题。这是该组件的代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var storyCards:ArrayCollection;
private function updateStory():void
{
trace("success");
}
]]>
</mx:Script>
<mx:TileList dataProvider="{storyCards}" >
<mx:itemRenderer>
<mx:Component>
<mx:HBox>
<mx:Label />
<mx:TextInput keyUp="updateStory()" />
<mx:TextArea text="{data.notes}" />
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
</mx:Canvas>
任何人都可以指出我正确的方向吗?
I'm hacking my way through learning Flex and have found some strange behaviour. When I try to compile my code, I'm thrown this error - Error: Call to a possibly undefined method updateStory. I've used method calls in this way before, and can't spot what's going wrong in this case. Here's the code for the component:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var storyCards:ArrayCollection;
private function updateStory():void
{
trace("success");
}
]]>
</mx:Script>
<mx:TileList dataProvider="{storyCards}" >
<mx:itemRenderer>
<mx:Component>
<mx:HBox>
<mx:Label />
<mx:TextInput keyUp="updateStory()" />
<mx:TextArea text="{data.notes}" />
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
</mx:Canvas>
Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在 mx:Component 父标签上。
来自文档:
因此,您需要将“updateStory”公开并添加outerdocument关键字,如下所示:
the problem is with the mx:Component parent tag.
from the docs:
So you need to make 'updateStory' public and add the outerdocument keyword, like so:
您还可以从 ItemRenderer 组件分派事件,并在主文档中添加侦听器。如果您想要将 ItemRenderer 组件移植到单独的 MXML 组件文件,这非常有用。
这是您的代码:
以下是您在单独的 MXML 组件中使用它的方法:
You could also dispatch an Event from the ItemRenderer Component, and add a Listener in the main document. This is useful should you want to port the ItemRenderer Component to a separate MXML Component file.
Here it is with your code:
Here's how you would use it in a separate MXML Component: