Flex - Problems in accessing static variable on another mxml page

隐身守侯 提交于 2019-12-02 08:43:16

I can't figure out the relationship between First.mxml and Second.mxml from your question. However, the following code can't access First.mxml.

Alert.show(First.CERT_LOAD_DATE);

Because the "First" is not the same instance as loaded "First.mxml".

How about to use singleton? It's accessible from anywhere.
1st, add MySingleton.as class like this.

package foo.bar
{
    public class MySingleton
    {
        private var _cert_load_date:String;

        public function MySingleton(internally:SingletonInternal)
        {
            super();
            if(internally == null)
            {
                throw new Error("Please use getInstance() method.");
            }
        }
        public static function getInstance():MySingleton
        {
            return SingletonInternal.instance;
        }

        public function set cert_load_date(value:String):void
        {
            _cert_load_date = value;
        } 

        public function get cert_load_date():String
        {
            return _cert_load_date; 
        }
    }
}
import foo.bar.MySingleton;

class SingletonInternal{
    public static var instance:MySingleton
        = new MySingleton(new SingletonInternal());
    public function SingletonInternal(){}
}

How to use

Set value at First.mxml.

public var singleton: MySingleton = MySingleton.getInstance();
private function changeManagerStatus():void
{
    singleton.cert_load_date = G2_CRTLoadDate.selectedDate.toDateString();
}

Second.mxml

public var singleton: MySingleton = MySingleton.getInstance();
private function closeHandler(evt:DropdownEvent):void {
    var CurrentDate:Date = new Date();
    if(General_Release_Dates.selectedLabel.toString() == "TBD")
    {         
        Alert.show(singleton.cert_load_date);  
    }
}

Updated: Aug 27 10:00(JST)

I think there are two way to change First.mxml's element using singleton.

1) Binding the DateField value to singleton variables, and clear the value at Secend.mxml.
2) Assign to singleton variables whole "First", and control from Second.mxml.

I'll write here the 2nd way. If you use this way, anything is controlable from Second.mxml.

MySingleton.as

private var _first:Object;

public function set first(value:Object):void
{
    _first = value;
} 

public function get first():Object
{
    return _first; 
}

First.mxml

singleton.first = this;

Second.mxml

public function something(): void{
    First(singleton.first).G2_CRTLoadDate.selectedDate = null;

    // The cast is unnecessary. Following code also works.
    // singleton.first.G2_CRTLoadDate.selectedDate = null;
}

Also you can execute First.mxml's public function from Second.mxml.

singleton.first.someFunctionDefinedAtFirst();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!