在 mxml 组件之间共享变量
我的应用程序中有多个 mxml 组件,所有这些组件都需要名为 genericX
的同一变量。我已将该变量包含在主 mxml 中并将其公开,
[Bindable] public var genericX:Number = 102;
但我仍然无法从其他 mxml 组件访问它。例如,如果我尝试这样做,它无法识别该变量。
<s:Button x="{genericX}" label="Click" />
I have several mxml components in an app, all of which need the same variable called genericX
. I've included that variable in the main mxml and made it public
[Bindable] public var genericX:Number = 102;
but I still can't access it from other mxml components. If I try to do this for example, it doesn't recognize the variable.
<s:Button x="{genericX}" label="Click" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
还有一个肮脏的解决方案,虽然有效,但效果不佳。您可以针对应用程序类创建静态变量。例如:
您可以从任何地方访问它,如下所示:
它不漂亮,但它确实有效:)
simon
There's also a filthy solution that works but isn't nice. You can create a static variable against the application class. For example:
You can access that from anywhere like this:
It ain't pretty, but it does work :)
simon
您无法通过这种方式访问。 Flex 中有一个名为
Events
的东西,您需要使用eventDispatcher
将 MXML 文件中的此变量传递给另一个文件。例如
[Bindable] public var genericX:Number = 102;
现在您需要进入要接收此事件的 MXML 组件,并使用 addEventListner() 接收此事件和相应的变量。
最后将其注入到您的按钮中。
You cannot access in this way. There is something called
Events
in Flex and you need to pass this variable in a MXML file to another usingeventDispatcher
.For example
[Bindable] public var genericX:Number = 102;
Now you need to get into the MXML component where you want to recieve this Event and using addEventListner() to recieve this event and the corresponding variable.
Then finally Inject it into your button.
您应该能够使用以下方式访问任何全局变量:
Flex 3:
Flex 4(看起来像您正在使用的):
然后:
You should be able to access any global variables with:
Flex 3:
Flex 4(looks like what you're using):
And then:
这里是一个在
MXML
组件之间共享变量的示例,方法是将变量声明为公共应用。Here is an example for sharing variables between
MXML
components by declaring them public in the main application.