从外部 AS 访问 MXML 中定义的元素
我有一个带有表单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要创建对 TextInputs 父容器实例的引用,然后使用该引用来访问 TextInputs 及其属性。 我认为我们需要对您的文件结构进行一些澄清。 您如何创建父容器的实例? 我想这就是你需要做的:
MyForm.mxml:
SaveVOContainer.as:
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:
SaveVOContainer.as:
在 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.
在 Flex 中执行代码隐藏的规范方法是通过继承。 以下是文档中的一个很好的解释: http://learn.adobe.com/wiki /display/Flex/Code+Behind。 简而言之:
所以,您的ActionScript 文件:
以及相应的 MXML 文件:
其他 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:
So, your ActionScript file:
And the corresponding MXML file:
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).
将其导入.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';
您的 mxml 文件中是否有指向 ActionScript 文件的脚本标记?
do you have a script tag in your mxml file that points to your ActionScript file?