Flex - Problems in accessing static variable on another mxml page

橙三吉。 提交于 2019-12-02 15:01:10

问题


First.mxml - Contains a Datefield control as follows:

<mx:DateField  id="G2_CRTLoadDate" width="150" selectedDate="{modelProxy.G2_CRTLoadDate}" change="{modelProxy.G2_CRTLoadDate = event.currentTarget.selectedDate;changeManagerStatus()}"/>

I'm assigning this Datefield value to a static variable CERT_LOAD_DATE as follows(First.mxml):

[Bindable]
public static var CERT_LOAD_DATE:String = "";
private function changeManagerStatus():void
{
CERT_LOAD_DATE = G2_CRTLoadDate.selectedDate.toDateString();
}

Second.mxml -Here, I have a Combobox as follows:

<mx:ComboBox id="General_Release_Dates"
             selectedItem="{modelProxy.General_Release_Dates}"
             valueCommit="{model.General_Release_Dates = event.currentTarget.selectedItem;updateReleaseDate(event)}"
             change="{model.General_Release_Dates = event.currentTarget.selectedItem;updateReleaseDate(event)}" close="closeHandler(event);" includeInLayout="true" visible="true">
</mx:ComboBox>

Inside the closeHandler function, I'm trying to access the variable CERT_LOAD_DATE as follows:

private function closeHandler(evt:DropdownEvent):void {
    var CurrentDate:Date = new Date();
    if(General_Release_Dates.selectedLabel.toString() == "TBD")
    {         
        Alert.show(First.CERT_LOAD_DATE);  
    }
}

The alert box displays no value (null). Please help.


回答1:


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


来源:https://stackoverflow.com/questions/32220118/flex-problems-in-accessing-static-variable-on-another-mxml-page

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