我无法从你的问题中找出 First.mxml 和 Second.mxml 之间的关系。
但是,以下代码无法访问First.mxml。
Alert.show(First.CERT_LOAD_DATE);
因为“First”与加载的“First.mxml”不是同一个实例。
使用单例怎么样?它可以从任何地方访问。
1,像这样添加 MySingleton.as 类。
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();
}
第二个.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;
}
首先.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 的公共函数。
singleton.first.someFunctionDefinedAtFirst();