开发者

Sharing variables between mxml components

开发者 https://www.devze.com 2023-01-02 01:29 出处:网络
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

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="{g开发者_如何学PythonenericX}" label="Click" />


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


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.


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" />


x="{parentApplication.genericX}"


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

0

精彩评论

暂无评论...
验证码 换一张
取 消