1 Reply Latest reply on Nov 25, 2011 8:42 PM by Flex harUI

    Why can't I convert module to itself's type

    vaaan pan

      Here is the code.

      In "DemoModule.mxml", there is a simple module with a textinput and a accessor.

      In "DemoModuleLoadForm.mxml", there is some buttons. One is to load module, the other two buttons are to set and get variable in the module.

       

      DemoModule.mxml

      <?xml version="1.0" encoding="utf-8"?>

      <s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"

          xmlns:s="library://ns.adobe.com/flex/spark"

          xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">

      <fx:Script>

        <![CDATA[

         [Bindable]

         private var _content:String;

        

         public function set content(value:String):void{

          this._content=value;

         }

        

         public function get content():String{

          return this._content;

         }

        ]]>

      </fx:Script>

      <s:Panel height="100%" width="100%">

        <s:TextInput text="@{_content}" width="200"/>

      </s:Panel>

      </s:Module>

      DemoModuleLoadForm.mxml

      <?xml version="1.0" encoding="utf-8"?>

      <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"

         xmlns:s="library://ns.adobe.com/flex/spark"

         xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">

      <fx:Script>

        <![CDATA[

         import mx.controls.Alert;

         import mx.core.IVisualElement;

         import mx.events.ModuleEvent;

         import mx.modules.IModuleInfo;

         import mx.modules.ModuleManager;

        

         private var _moduleInfo:IModuleInfo;

         private var module:Object;

        

         protected function button1_clickHandler(event:MouseEvent):void

         {

          _moduleInfo=ModuleManager.getModule("demo/demomodule/DemoModule.swf");

          _moduleInfo.addEventListener(ModuleEvent.READY, moduleLoadHandler);

          _moduleInfo.load();

         }

        

         private function moduleLoadHandler(evt:ModuleEvent):void

         {

          this.main.removeAllElements();

          module=_moduleInfo.factory.create();

          this.main.addElement(module as IVisualElement);

         }

        

         protected function btnChangeContent_clickHandler(event:MouseEvent):void

         {

          module.content=String(new Date());

          // Please Notice Here, when I use the code below instead, when click "Load Module" button, the system will fire a exception

          // (module as DemoModule).content=String(new Date());

         }

        

         protected function btnGetContent_clickHandler(event:MouseEvent):void

         {

          Alert.show(module.content);

         }

        

        ]]>

      </fx:Script>

      <s:Panel title="Some Title" width="100%" height="100%">

        <s:layout>

         <s:VerticalLayout/>

        </s:layout>

        <s:Group width="100%">

         <s:layout>

          <s:HorizontalLayout/>

         </s:layout>

         <s:Button label="Load Module" click="button1_clickHandler(event)"/>

         <s:Button label="Change Value" id="btnChangeContent" click="btnChangeContent_clickHandler(event)"/>

         <s:Button label="Get Value" id="btnGetContent" click="btnGetContent_clickHandler(event)"/>

        </s:Group>

        <s:Group id="main" width="100%" height="100%"/>

      </s:Panel>

      </s:Group>