Flex的 - 在另一个MXML页面访问静态变量的问题 [英] Flex - Problems in accessing static variable on another mxml page

查看:320
本文介绍了Flex的 - 在另一个MXML页面访问静态变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()}"/>

我赋予这个的DateField 值和一个静态变量的 CERT_LOAD_DATE 如下( First.mxml ):

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 ,尤其是圆形的,我有一个组合框,如下所示:

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>

是closeHandler 的功能,我想访问变量的 CERT_LOAD_DATE 如下:

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

}

警报框显示没有值(NULL)。请大家帮帮忙。

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

推荐答案

我无法从你的问题找出First.mxml和Second.mxml之间的关系。 但是,以下code不能访问First.mxml

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

由于第一是不一样的实例作为装First.mxml。

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

如何使用单身?这是从任何地方访问。
1,添加MySingleton.as类是这样的。

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

如何使用

在First.mxml设置值。

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

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

我觉得有两种方式使用单来更改First.mxml的元素。

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

1)绑定的DateField值独居的变量,并清除在Secend.mxml价值。
2)分配给从Second.mxml独居变量整个第一,和控制。

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.

我会写在这里的第二个途径。 如果你用这种方式,任何事情都是从Second.mxml可控。

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

MySingleton.as

MySingleton.as

private var _first:Object;

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

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

First.mxml

First.mxml

singleton.first = this;

Second.mxml

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的公共职能。

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

singleton.first.someFunctionDefinedAtFirst();

这篇关于Flex的 - 在另一个MXML页面访问静态变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆