Actionscript 如何将变量值从一个MXML传递到另一个MXML?
我正在用ActionScript在FlashBuilder中做一个项目 我有两个MXML文件:login.MXML和welcome.MXML login.mxml:Actionscript 如何将变量值从一个MXML传递到另一个MXML?,actionscript,mxml,Actionscript,Mxml,我正在用ActionScript在FlashBuilder中做一个项目 我有两个MXML文件:login.MXML和welcome.MXML login.mxml: var username:String="sample"; var password:String="sample"; welcome.mxml: trace("welcome"+username); // o/p-welcome sample 我必须将用户名值从login.mxml传递到welcome.xml。 是否可以将变量
var username:String="sample";
var password:String="sample";
welcome.mxml:
trace("welcome"+username); // o/p-welcome sample
我必须将用户名值从login.mxml传递到welcome.xml。
是否可以将变量值从一个MXML传递到另一个MXML文件?怎么做?是的,这是可能的。不过,最好的方法是将视图绑定到对象值 您的视图似乎没有“一个聚合另一个”的关联,但它们的父视图(容器视图)两者都知道。因此,父对象将向这两个视图传递对象引用,当更新此实例时,将通过数据绑定通知和更新视图 如果视图彼此完全独立,那么通过应用程序发送事件将是最直接的方法。您应该引入由应用程序调度的新事件类型(即SystemEvent)。为了使您的应用程序不受视图中使用的特定全局变量的过多引用的影响,如果您还需要使用MVC,我建议您派一名代表:
package de.guj.vila.delegates {
import flash.events.Event;
import flash.events.IEventDispatcher;
import mx.core.FlexGlobals;
import mx.core.IMXMLObject;
import mx.core.UIComponent;
public class ViewDelegate implements IEventDispatcher, IMXMLObject {
//---------------------------------------------------------------------
//
// Properties
//
//---------------------------------------------------------------------
private var _bus:IEventDispatcher;
private var _uiComponent:UIComponent;
/**
* The view which uses the delegate.
*/
public function set uiComponent(value:UIComponent):void {
_uiComponent = value;
}
//---------------------------------------------------------------------
//
// Constructor
//
//---------------------------------------------------------------------
public function ViewDelegate() {
_bus = FlexGlobals.topLevelApplication as IEventDispatcher;
}
//---------------------------------------------------------------------
//
// Implemented Methods
//
//---------------------------------------------------------------------
/**
* @inheritDoc
*
* @see flash.events.IEventDispatcher
*/
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
_bus.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
/**
* @inheritDoc
* @see flash.events.IEventDispatcher
*/
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
_bus.removeEventListener(type, listener, useCapture);
}
/**
* @inheritDoc
* @see flash.events.IEventDispatcher
*/
public function dispatchEvent(event:Event):Boolean {
return _bus.dispatchEvent(event);
}
/**
* @inheritDoc
*
* @see flash.events.IEventDispatcher
*/
public function hasEventListener(type:String):Boolean {
return _bus.hasEventListener(type);
}
/**
* @inheritDoc
*
* @see mx.core.IMXMLObject
*/
public function initialized(document:Object, id:String):void {
uiComponent = document as UIComponent;
}
/**
* @inheritDoc
*
* @see flash.events.IEventDispatcher
*/
public function willTrigger(type:String):Boolean {
return _bus.willTrigger(type);
}
}
}
您只需在fx:Declarations块中填充,给它一个id,并在视图之间调度事件。你只需要设置听众。这样,您就可以很容易地实现一个非常干净的结构,因为您只需重构委托。利用委托作为基类,您可以处理委托中的任何事件,因此视图保持干净,最重要的是,很容易移植到不同的MVC方法,因为您已经将视图行为与应用程序行为隔离开来
最后,您需要使用MVC框架(例如RobotLegs)来轻松地扩展应用程序