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

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

问题描述

First.mxml - 包含如下日期字段控件:

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

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

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

推荐答案

我无法从您的问题中弄清楚 First.mxml 和 Second.mxml 之间的关系.但是下面的代码无法访问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"与加载的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();
}

第二个.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)将整个First"分配给单例变量,并从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; 
}

第一个.mxml

singleton.first = this;

第二个.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天全站免登陆