这是在 mxml 中调用组件方法的正确方法吗

发布于 2024-08-09 03:05:15 字数 916 浏览 3 评论 0原文

我正在通过学​​习 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 技术交流群。

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

发布评论

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

评论(2

記憶穿過時間隧道 2024-08-16 03:05:15

问题出在 mx:Component 父标签上。

来自文档:

<mx:Component>标签定义了一个新的
MXML 文件内的范围,其中
项目渲染器的本地范围或
项目编辑器由 MXML 定义
代码块由
<mx:组件>和 </mx:Component>
标签。访问外部元素
项目渲染器的本地范围
或项目编辑器,您为元素添加前缀
使用outerDocument 关键字命名。

因此,您需要将“updateStory”公开并添加outerdocument关键字,如下所示:

<?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;

    public function updateStory():void
    {
       trace("success");
    }       
    ]]>
</mx:Script>
<mx:TileList dataProvider="{storyCards}" >
       <mx:itemRenderer>
            <mx:Component>
                <mx:HBox>
                 <mx:Label />
                 <mx:TextInput keyUp="outerDocument.updateStory()" />
                 <mx:TextArea text="{data.notes}" />
                </mx:HBox>
            </mx:Component>
        </mx:itemRenderer>
    </mx:TileList>
</mx:Canvas>

the problem is with the mx:Component parent tag.

from the docs:

The <mx:Component> tag defines a new
scope within an MXML file, where the
local scope of the item renderer or
item editor is defined by the MXML
code block delimited by the
<mx:Component> and </mx:Component>
tags. To access elements outside of
the local scope of the item renderer
or item editor, you prefix the element
name with the outerDocument keyword.

So you need to make 'updateStory' public and add the outerdocument keyword, like so:

<?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;

    public function updateStory():void
    {
       trace("success");
    }       
    ]]>
</mx:Script>
<mx:TileList dataProvider="{storyCards}" >
       <mx:itemRenderer>
            <mx:Component>
                <mx:HBox>
                 <mx:Label />
                 <mx:TextInput keyUp="outerDocument.updateStory()" />
                 <mx:TextArea text="{data.notes}" />
                </mx:HBox>
            </mx:Component>
        </mx:itemRenderer>
    </mx:TileList>
</mx:Canvas>
淡笑忘祈一世凡恋 2024-08-16 03:05:15

您还可以从 ItemRenderer 组件分派事件,并在主文档中添加侦听器。如果您想要将 ItemRenderer 组件移植到单独的 MXML 组件文件,这非常有用。

这是您的代码:

<?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}" myEvent="updateStory();">

        <mx:itemRenderer>

          <mx:Component>
            <mx:Metadata>
                [Event(name="myEvent", type="flash.events.Event")]
            </mx:Metadata>

        <mx:HBox>
          <mx:Label />
          <mx:TextInput keyUp="dispatchEvent(new Event('myEvent', true))" />
          <mx:TextArea text="{data.notes}" />
        </mx:HBox>

          </mx:Component>

        </mx:itemRenderer>

  </mx:TileList>
</mx:Canvas>

以下是您在单独的 MXML 组件中使用它的方法:

<?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}" myEvent="updateStory();" itemRenderer="StoryEditor" />
</mx:Canvas>

StoryEditor.mxml:

<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Metadata>
        [Event(name="myEvent", type="flash.events.Event")]
    </mx:Metadata>

    <mx:Label />

    <mx:TextInput keyUp="dispatchEvent(new Event('myEvent', true));" />

    <mx:TextArea text="{data.notes}" />
</mx:HBox>

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:

<?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}" myEvent="updateStory();">

        <mx:itemRenderer>

          <mx:Component>
            <mx:Metadata>
                [Event(name="myEvent", type="flash.events.Event")]
            </mx:Metadata>

        <mx:HBox>
          <mx:Label />
          <mx:TextInput keyUp="dispatchEvent(new Event('myEvent', true))" />
          <mx:TextArea text="{data.notes}" />
        </mx:HBox>

          </mx:Component>

        </mx:itemRenderer>

  </mx:TileList>
</mx:Canvas>

Here's how you would use it in a separate MXML 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}" myEvent="updateStory();" itemRenderer="StoryEditor" />
</mx:Canvas>

StoryEditor.mxml:

<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Metadata>
        [Event(name="myEvent", type="flash.events.Event")]
    </mx:Metadata>

    <mx:Label />

    <mx:TextInput keyUp="dispatchEvent(new Event('myEvent', true));" />

    <mx:TextArea text="{data.notes}" />
</mx:HBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文