从外部 AS 访问 MXML 中定义的元素

发布于 2024-07-14 10:37:51 字数 971 浏览 9 评论 0原文

我有一个带有表单的 MXML,其中有两个 TextInput。 我讨厌 MXML 文件中包含任何代码(我来自 JavaScript 结构),因此我使用标签

mx:Script source="external.as"

来包含任何 MXML 文件中使用的任何代码。 问题是,如果我在 external.as 文件中包含以下代码:

private function populateFromForm():void{
   var vo:ValidObject= new ValidObject();
   vo.market = marketInput.text;
   vo.segment = segmentInput.text;
   vo.priceLow = priceLowInput.text;
   vo.priceHigh = priceHighInput.text;
}

其中 marketInput、segmentInput、priceLowInput 和 PriceHighInput 是 MXML 文件中定义的 TextInput。 当我尝试编译时,我得到一个 1120: Access to undefined property XXXXX

我尝试在函数之前添加此行:

public var marketInput:TextInput;
public var segmentInput:TextInput;
public var priceLowInput:TextInput;
public var priceHighInput:TextInput;

但是我得到一个 1151:Aconflict isn't indefinition XXXX in内部命名空间 这非常有意义。

有没有一种方法可以做到这一点,而不必将所有输入引用作为函数的参数传递给函数?

I have a MXML with a form, and inside it, two TextInputs. I hate having any piece of code inside the MXML file (I come from a JavaScript formation) so I use a

mx:Script source="external.as"

tag to include any code used in any MXML file. The problem is that if I have this code on the external.as file:

private function populateFromForm():void{
   var vo:ValidObject= new ValidObject();
   vo.market = marketInput.text;
   vo.segment = segmentInput.text;
   vo.priceLow = priceLowInput.text;
   vo.priceHigh = priceHighInput.text;
}

Where marketInput, segmentInput, priceLowInput and priceHighInput are TextInputs defined in the MXML file. When I try to complile I get a 1120: Access to undefined property XXXXX

I have tried adding this lines prior to the function:

public var marketInput:TextInput;
public var segmentInput:TextInput;
public var priceLowInput:TextInput;
public var priceHighInput:TextInput;

But instead I get a 1151:A conflict exists with definition XXXX in namespace internal which makes perfect sense.

Is there a way to do this without having to pass all the input references to the function as parameters of it?

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

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

发布评论

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

评论(5

剑心龙吟 2024-07-21 10:37:51

您需要创建对 TextInputs 父容器实例的引用,然后使用该引用来访问 TextInputs 及其属性。 我认为我们需要对您的文件结构进行一些澄清。 您如何创建父容器的实例? 我想这就是你需要做的:

MyForm.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:TextInput id="marketInput" />
    <mx:TextInput id="segmentInput" />
    <mx:TextInput id="priceLowInput" />
    <mx:TextInput id="priceHighInput" />
</mx:VBox>

SaveVOContainer.as:

package
{
    public class SaveVoContainer extends Container
    {
        private var myForm:MyForm = new MyForm();

        public function SaveVOContainer
        {
            this.addChild(myForm);
        }

        private function populateFromForm():void{
           var vo:ValidObject= new ValidObject();
           vo.market = myForm.marketInput.text;
           vo.segment = myForm.segmentInput.text;
           vo.priceLow = myForm.priceLowInput.text;
           vo.priceHigh = myForm.priceHighInput.text;
        }
    }
}

You need to create a reference to an instance of the TextInputs' parent container, and then use that reference to accsess the TextInputs and their properties. I think we need some clarification on your file structure. How are you creating the instance of the parent container? I'm thinking this is what you need to do:

MyForm.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:TextInput id="marketInput" />
    <mx:TextInput id="segmentInput" />
    <mx:TextInput id="priceLowInput" />
    <mx:TextInput id="priceHighInput" />
</mx:VBox>

SaveVOContainer.as:

package
{
    public class SaveVoContainer extends Container
    {
        private var myForm:MyForm = new MyForm();

        public function SaveVOContainer
        {
            this.addChild(myForm);
        }

        private function populateFromForm():void{
           var vo:ValidObject= new ValidObject();
           vo.market = myForm.marketInput.text;
           vo.segment = myForm.segmentInput.text;
           vo.priceLow = myForm.priceLowInput.text;
           vo.priceHigh = myForm.priceHighInput.text;
        }
    }
}
网名女生简单气质 2024-07-21 10:37:51

在 Flex 中进行“代码隐藏”是很痛苦的。 没有像 Javascript 那样的分部类的概念或原型继承的灵活性。 Google 搜索“flex 中的代码隐藏”以获取许多资源。

我认为您最好习惯在 mxml 中嵌入代码的想法。 使用脚本标签尽可能避免内联代码。 如果您必须在 MXML 中编写大量代码,也许您可​​能希望将代码重构为多个自定义组件。 如果它们可以重复使用,则可以获得奖励积分。

Doing a "code-behind" is painful in Flex. There is no concept of partial classes or the flexibility of prototypal inheritance as in Javascript. Google for "code-behind in flex" for many resources.

I think it's better you get used to the idea of embedding code in mxml. Use script tags avoiding inline code as much as possible. If you have to write a lot of code within MXML, perhaps you may want to re-factor the code into multiple custom components. Bonus points if they are reusable.

柒七 2024-07-21 10:37:51

在 Flex 中执行代码隐藏的规范方法是通过继承。 以下是文档中的一个很好的解释: http://learn.adobe.com/wiki /display/Flex/Code+Behind。 简而言之:

  1. 声明一个 ActionScript 类用作基类。
  2. 将基类设置为 MXML 文件中的根容器。
  3. 对于 MXML 文件中声明的任何控件,您必须使用完全相同的名称将它们重新声明为基类的公共成员(与上面使用源标记的脚本块所做的完全相同,只有它有效:-)

所以,您的ActionScript 文件:

package mypackage
{
    import mx.controls.TextInput;

    public class myClass extends WindowedApplication
    {
        public var marketInput:TextInput;

        private function populateFromForm():void{
            /* As above */
        }
    }
}

以及相应的 MXML 文件:

<?xml version="1.0" encoding="utf-8"?>
<custom:myClass xmlns:custom="mypackage.*"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <mx:TextInput id="marketInput"/>
</custom:myClass>

其他 TextInput 控件的等等。 现在您的 populateFromForm 函数应该可以工作了。

必须两次重新声明相同的实体是令人发指的,但这并不完全是先前受访者所说的那样(尽管 Flex 4 中的这一点可能会发生变化,使其比以前不那么痛苦)。

The canonical way to do code-behind in Flex is via inheritance. Here's a good explanation from the docs: http://learn.adobe.com/wiki/display/Flex/Code+Behind. In a nutshell:

  1. Declare an ActionScript class to use as your base class.
  2. Set the base class as the root container in your MXML file.
  3. For any controls declared in your MXML file, you have to redeclare them as public members of the base class using the exact same name (exactly as you are doing above for your script block with source tag, only it works :-)

So, your ActionScript file:

package mypackage
{
    import mx.controls.TextInput;

    public class myClass extends WindowedApplication
    {
        public var marketInput:TextInput;

        private function populateFromForm():void{
            /* As above */
        }
    }
}

And the corresponding MXML file:

<?xml version="1.0" encoding="utf-8"?>
<custom:myClass xmlns:custom="mypackage.*"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <mx:TextInput id="marketInput"/>
</custom:myClass>

Et cetera for your other TextInput controls. And now your populateFromForm function should work.

It is kind of heinous to have to redeclare the same entities twice, but it's not quite the bag of hurt the earlier respondent made it out to be (although it's possible this changed in Flex 4 to make it less painful than it was).

雨落□心尘 2024-07-21 10:37:51
  • 将其导入.AS:

    import mx.core.Application;

  • 在 .AS 中使用此:

    mx.core.Application.application.component.property = 值;
    mx.core.Application.application.myText.text = 'test';

  • import this in .AS:

    import mx.core.Application;

  • in the .AS use this:

    mx.core.Application.application.component.property = value;
    mx.core.Application.application.myText.text = 'test';

那片花海 2024-07-21 10:37:51

您的 mxml 文件中是否有指向 ActionScript 文件的脚本标记?


<mx:Script source='includes/foo.as' />

do you have a script tag in your mxml file that points to your ActionScript file?


<mx:Script source='includes/foo.as' />

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