0

我正在使用 ActionScript 在 Flash Builder 中做一个项目。

我有两个 MXML 文件:login.mxml 和 welcome.mxml。

登录.mxml:

var username:String="sample";
var password:String="sample";

欢迎.mxml:

trace("welcome"+username); // o/p-welcome sample

我必须将用户名值从 login.mxml 传递给 welcome.xml。是否可以将变量值从一个 MXML 传递到另一个 MXML 文件?如何?

4

1 回答 1

0

对的,这是可能的。最好的方法是将视图绑定到对象值。

您的视图似乎与“一个聚合另一个”没有关联,但它们的父级(容器视图)两者都知道。因此,父级会将对象引用传递给两个视图,并且当更新此实例时,将通过数据绑定通知和更新视图。

如果视图彼此完全独立,那么通过应用程序通过应用程序分派事件将是最直接的方式。您应该引入由应用程序调度的新事件类型(即 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)来轻松扩展您的应用程序。

于 2012-12-20T09:28:30.460 回答