应用IMXMLObject自定义功能性组件

在flex项目中,应用到非显示的功能性组件,一种方法是直接定义actionScript类。另外一种方法是实现IMXMLObject接口。实现IMXMLObject接口的好处,是可以直接调用功能函数。

IMXMLObject中的方法function initialized(document:Object, id:String):void,在IMXMLObject置入MXML时被直接调用,类似于触发了initialized事件。如以下示例:

TempCom.as

package component {

    import flash.display.Shape;
    
import mx.core.IMXMLObject;
    
    
public class TempCom implements IMXMLObject{
        
public var id:String="tempCom";
        
public var rect:Shape;
        
        
public var paramOne:String="paramOne";
        
public var paramTwo:String="paramTwo";
      

        
public function executSome(para1:String,para2:String):void{
            trace("para1:->"+para1,"para2:->"+para2,"from executSome");
        }
        
public function initialized(document:Object, id:String):void
        {
            rect=createShape();
            executSome(paramOne,paramTwo);
        }
        
private function createShape():Shape{
            var rect:Shape=new Shape;
            rect.graphics.beginFill(0xffccff);
            rect.graphics.drawRect(0,0,100,100);
            rect.graphics.endFill();
            
return rect;
        }
   
 }
}

MainApp.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s
="library://ns.adobe.com/flex/spark" 
               xmlns:mx
="library://ns.adobe.com/flex/mx" 
               xmlns:com
="component.*"
               xmlns:mxmlCom
="mxmlCom.*"
               minWidth
="955" minHeight="600"
               creationComplete
="application1_creationCompleteHandler(event)">
    
<fx:Script>
        
<![CDATA[
            import mx.events.FlexEvent;
            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                container.addChild(tempCom.rect);
            }
            
        
]]>
    
</fx:Script>
    
<fx:Declarations>
        
<com:TempCom  id="tempCom" paramOne="aaa" paramTwo="bbbb"/>
    
</fx:Declarations>
    
<mx:UIComponent id="container"/>

</s:Application>  


运行结果会显示出一个正方形,并输出:" para1:->aaa para2:->bbbb from executSome ".

原文地址:https://www.cnblogs.com/fxair/p/2114050.html