如何访问一个MXML文件的根元素,如果我不能设置一个id?元素、文件、MXML、id

2023-09-08 11:58:17 作者:潇湘夜雨

如果我想要做这样的事情:

If I wanted to do something like this:

<mx:Canvas  xmlns:mx="http://www.adobe.com/2006/mxml" 
            horizontalScrollPolicy="off" 
            verticalScrollPolicy="off"  
            xmlns:view="com.foo.bar.view.*" 
>
    <mx:Script>
      <![CDATA[
        myWidth = 100;
        myHeight = 200;
        myCanvas.width = myWidth;
        myCanvas.height = myHeight;
      ]]>
    </mx:Script>
</mx:Canvas>

我将如何获得myCanvas(这里我想myCanvas是根)的手柄?

How would I get a handle on myCanvas (where I'd want myCanvas to be the root )?

推荐答案

从内部MXML文件访问由根节点指定的组件,你可以使用关键词。一个MXML内的任何code。在对象的上下文中运行 - 你可以同时省略关键字,如果你没有被任何同名的局部变量。

To access the component specified by the root node from within an mxml file, you can use this keyword. Any code inside an mxml runs in the context of this object - you can as well omit the keyword if you don't have any local variable by the same name.

this.width = myWidth;
this.height = myHeight;

有关你的第二个问题:

假设你的MXML文件的名称是 MyCanvas.mxml 。 MyCanvas /&GT; 标签:NS;你会使用&LT将它添加到另一个组件。您可以设置一个ID存在,并使用该访问它。

Let's say your mxml file's name is MyCanvas.mxml. You'd add this to another component using <ns:MyCanvas/> tag. You can set an id there and access it using that.

<ns:MyCanvas id="myCanvas"/>

里面的脚本:

Inside script:

myCanvas.width = whatever;