在 mxml 组件之间共享变量

发布于 2024-09-03 02:06:22 字数 285 浏览 8 评论 0原文

我的应用程序中有多个 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 技术交流群。

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

发布评论

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

评论(5

往昔成烟 2024-09-10 02:06:22

还有一个肮脏的解决方案,虽然有效,但效果不佳。您可以针对应用程序类创建静态变量。例如:

[Bindable] public static var genericX : Object

您可以从任何地方访问它,如下所示:

MyApplicationName.genericX

它不漂亮,但它确实有效:)

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:

[Bindable] public static var genericX : Object

You can access that from anywhere like this:

MyApplicationName.genericX

It ain't pretty, but it does work :)

simon

缱绻入梦 2024-09-10 02:06:22

您无法通过这种方式访问​​。 Flex 中有一个名为 Events 的东西,您需要使用 eventDispatcher 将 MXML 文件中的此变量传递给另一个文件。

例如

[Bindable] public var genericX:Number = 102;

private function init():void {

var evt:NewCustomEvent = new CustomEvent(CustomEvent.SENDDATA);
evt.genericaValue = genericX
dispatchEvent(evt);

}

现在您需要进入要接收此事件的 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 using eventDispatcher.

For example

[Bindable] public var genericX:Number = 102;

private function init():void {

var evt:NewCustomEvent = new CustomEvent(CustomEvent.SENDDATA);
evt.genericaValue = genericX
dispatchEvent(evt);

}

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.

场罚期间 2024-09-10 02:06:22

您应该能够使用以下方式访问任何全局变量:

Flex 3:

var app:Application = mx.core.Application.application as Application;

Flex 4(看起来像您正在使用的):

var app:Object = FlexGlobals.topLevelApplication;

然后:

<s:Button x="{app.genericX}" label="Click" />

You should be able to access any global variables with:

Flex 3:

var app:Application = mx.core.Application.application as Application;

Flex 4(looks like what you're using):

var app:Object = FlexGlobals.topLevelApplication;

And then:

<s:Button x="{app.genericX}" label="Click" />
柳絮泡泡 2024-09-10 02:06:22
x="{parentApplication.genericX}"
x="{parentApplication.genericX}"
淡淡離愁欲言轉身 2024-09-10 02:06:22

这里是一个在 MXML 组件之间共享变量的示例,方法是将变量声明为公共应用。

Here is an example for sharing variables between MXML components by declaring them public in the main application.

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