Flex - Problems in accessing static variable on another mxml page

后端 未结 1 815
情书的邮戳
情书的邮戳 2021-01-27 19:03

First.mxml - Contains a Datefield control as follows:



        
1条回答
  •  再見小時候
    2021-01-27 19:21

    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();
    

    0 讨论(0)
提交回复
热议问题