Flex - 在另一个 mxml 页面上访问静态变量时出现问题

Flex - Problems in accessing static variable on another mxml page

First.mxml - 包含一个 Datefield 控件如下:

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

我将此 Datefield 值分配给静态变量 CERT_LOAD_DATE,如下所示(First.mxml):

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

Second.mxml -在这里,我有一个 Combobox 如下:

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

closeHandler 函数中,我尝试按如下方式访问变量 CERT_LOAD_DATE

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

警告框不显示任何值(空)。请帮忙。

我无法从你的问题中找出 First.mxml 和 Second.mxml 之间的关系。 但是,下面的代码无法访问First.mxml.

Alert.show(First.CERT_LOAD_DATE);

因为 "First" 与加载的实例 "First.mxml" 不同。

使用单例怎么样?它可以从任何地方访问。
1、添加 MySingleton.as class 像这样。

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(){}
}

使用方法

将值设为 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);  
    }
}

更新时间:8 月 27 日 10:00(日本标准时间)

我认为有两种方法可以使用单例更改 First.mxml 的元素。

1) 将DateField值绑定到单例变量,并清除Secend.mxml.
处的值 2) 将整个 "First" 分配给单例变量,并从 Second.mxml.

控制

这里写第二种方式。 如果你使用这种方式,任何东西都可以从 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;
}

您还可以从 Second.mxml.

执行 First.mxml 的 public 函数
singleton.first.someFunctionDefinedAtFirst();