如何在mxml应用程序中将其用作类代码?

时间:2011-05-09 09:32:35

标签: flex flex3 flex4 flexbuilder

我从我的一位朋友那里获取了这段代码,但实际上它完全是一团糟,我通过运行此代码得到了很多错误,我有一些问题

  1. 我如何在mxml应用程序中使用此代码,因为我们知道我们不能在mx脚本中使用公共类,所以有什么办法呢
  2. 你可以看到.mx_internal,我收到错误的说'在dot之前定义对象',因为我删除mx_internal并尝试使用import mx.binding.mx_internal并使用命名空间mx_internal应用程序而不是删除任何内容
  3. 现在你会告诉我其他方法很容易解决问题,但我的整个项目只是在这个方法上

    提前感谢您的帮助 我试图从过去15天解决这个问题,没有成功,请帮我下面是代码

    package components
    {
        import flash.events.*;
        import flash.utils.*;
        import mx.binding.*;
        import mx.containers.*;
        import mx.controls.*;
        import mx.core.*;
        import mx.events.*;
        import mx.styles.*;
    
        public class DialogTitle extends HBox implements IBindingClient
        {
            private var _110371416title:String = "Dialog Title";
            public var _DialogTitle_Image1:Image;
            public var _DialogTitle_Image2:Image;
            public var _DialogTitle_Label1:Label;
            var _bindingsBeginWithWord:Object;
            private var _1859425293showCloseButton:Boolean = false;
            var _bindingsByDestination:Object;
            var _watchers:Array;
            var _bindings:Array;
            private var _documentDescriptor_:UIComponentDescriptor;
            private static var _watcherSetupUtil:IWatcherSetupUtil;
    
            public function DialogTitle()
            {
                _documentDescriptor_ = new UIComponentDescriptor({type:HBox, propertiesFactory:function () : Object
                {
                    return {height:27, childDescriptors:[new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
                    {
                        return {width:5};
                    }// end function
                    }), new UIComponentDescriptor({type:Image, id:"_DialogTitle_Image1"}), new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
                    {
                        return {width:5};
                    }// end function
                    }), new UIComponentDescriptor({type:Label, id:"_DialogTitle_Label1", stylesFactory:function () : void
                    {
                        this.fontSize = 14;
                        this.color = 16777215;
                        this.fontWeight = "bold";
                        return;
                    }// end function
                    }), new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
                    {
                        return {percentWidth:100};
                    }// end function
                    }), new UIComponentDescriptor({type:Image, id:"_DialogTitle_Image2", events:{click:"___DialogTitle_Image2_click"}, propertiesFactory:function () : Object
                    {
                        return {useHandCursor:true, buttonMode:true, mouseChildren:false, toolTip:"Close"};
                    }// end function
                    }), new UIComponentDescriptor({type:Spacer, propertiesFactory:function () : Object
                    {
                        return {width:13};
                    }// end function
                    })]};
                }// end function
                });
                _bindings = [];
                _watchers = [];
                _bindingsByDestination = {};
                _bindingsBeginWithWord = {};
                mx_internal::_document = this;
                if (!this.styleDeclaration)
                {
                    this.styleDeclaration = new CSSStyleDeclaration();
                }
                this.styleDeclaration.defaultFactory = function () : void
                {
                    this.backgroundColor = 9947478;
                    this.horizontalGap = 0;
                    this.verticalAlign = "middle";
                    this.verticalGap = 0;
                    return;
                }// end function
                ;
                this.height = 27;
                this.percentWidth = 100;
                return;
            }// end function
    
            private function _DialogTitle_bindingExprs() : void
            {
                var _loc_1:* = undefined;
                _loc_1 = CustomEmbeddedAssets.logoImageSmall;
                _loc_1 = title;
                _loc_1 = EmbeddedAssets.dialogClose;
                _loc_1 = showCloseButton;
                return;
            }// end function
    
            public function get showCloseButton() : Boolean
            {
                return this._1859425293showCloseButton;
            }// end function
    
            override public function initialize() : void
            {
                var target:DialogTitle;
                var watcherSetupUtilClass:Object;
                .mx_internal::setDocumentDescriptor(_documentDescriptor_);
                var bindings:* = _DialogTitle_bindingsSetup();
                var watchers:Array;
                target;
                if (_watcherSetupUtil == null)
                {
                    watcherSetupUtilClass = getDefinitionByName("_components_DialogTitleWatcherSetupUtil");
                    var _loc_2:* = watcherSetupUtilClass;
                    _loc_2.watcherSetupUtilClass["init"](null);
                }
                _watcherSetupUtil.setup(this, function (param1:String)
                {
                    return target[param1];
                }// end function
                , bindings, watchers);
                var i:uint;
                while (i < bindings.length)
                {
    
                    Binding(bindings[i]).execute();
                    i = (i + 1);
                }
                mx_internal::_bindings = mx_internal::_bindings.concat(bindings);
                mx_internal::_watchers = mx_internal::_watchers.concat(watchers);
                super.initialize();
                return;
            }// end function
    
            public function get title() : String
            {
                return this._110371416title;
            }// end function
    
            private function _DialogTitle_bindingsSetup() : Array
            {
                var binding:Binding;
                var result:Array;
                binding = new Binding(this, function () : Object
                {
                    return CustomEmbeddedAssets.logoImageSmall;
                }// end function
                , function (param1:Object) : void
                {
                    _DialogTitle_Image1.source = param1;
                    return;
                }// end function
                , "_DialogTitle_Image1.source");
                result[0] = binding;
                binding = new Binding(this, function () : String
                {
                    var _loc_1:* = title;
                    var _loc_2:* = _loc_1 == undefined ? (null) : (String(_loc_1));
                    return _loc_2;
                }// end function
                , function (param1:String) : void
                {
                    _DialogTitle_Label1.text = param1;
                    return;
                }// end function
                , "_DialogTitle_Label1.text");
                result[1] = binding;
                binding = new Binding(this, function () : Object
                {
                    return EmbeddedAssets.dialogClose;
                }// end function
                , function (param1:Object) : void
                {
                    _DialogTitle_Image2.source = param1;
                    return;
                }// end function
                , "_DialogTitle_Image2.source");
                result[2] = binding;
                binding = new Binding(this, function () : Boolean
                {
                    return showCloseButton;
                }// end function
                , function (param1:Boolean) : void
                {
                    _DialogTitle_Image2.visible = param1;
                    return;
                }// end function
                , "_DialogTitle_Image2.visible");
                result[3] = binding;
                return result;
            }// end function
    
            public function set showCloseButton(param1:Boolean) : void
            {
                var _loc_2:* = this._1859425293showCloseButton;
                if (_loc_2 !== param1)
                {
                    this._1859425293showCloseButton = param1;
                    this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "showCloseButton", _loc_2, param1));
                }
                return;
            }// end function
    
            public function ___DialogTitle_Image2_click(event:MouseEvent) : void
            {
                dispatchEvent(new Event("onCancel"));
                parent.visible = false;
                return;
            }// end function
    
            public function set title(param1:String) : void
            {
                var _loc_2:* = this._110371416title;
                if (_loc_2 !== param1)
                {
                    this._110371416title = param1;
                    this.dispatchEvent(PropertyChangeEvent.createUpdateEvent(this, "title", _loc_2, param1));
                }
                return;
            }// end function
    
            public static function set watcherSetupUtil(param1:IWatcherSetupUtil) : void
            {
                DialogTitle._watcherSetupUtil = param1;
                return;
            }// end function
    
        }
    }
    

2 个答案:

答案 0 :(得分:1)

抛出此代码。此代码由mxmlc编译器从MXML类自动生成,不能在生产中使用。它适用于计算机,而不适用于人类。自己编写正确的代码。我希望它更清晰,更易于维护。是的,这段代码不是由你的朋友写的:)

答案 1 :(得分:0)

此文件是从.mxml文件反编译的,该文件使用标记来描述布局。但是,有些反编译器无法完全转换它,你会看到这样的文件。

如您所见,UIComponentDescriptor的参数是一个对象,它包含许多键 - 值对。它有一些键:

  1. type:类 - &gt;这是此组件的类型,它是.mxml文件的节点标记
  2. id:字符串 - &gt;这是节点的ID,也是您可以在<fx:Script> tag
  3. 中使用的变量名称
  4. event:对象 - &gt;组件将触发的事件
  5. stylesFactory:功能 - &gt;组件的样式设置
  6. propertiesFactory:功能 - &gt;包含一些属性和子节点
  7. 像这样的代码:

    <mx:Canvas id="mainCanvas" borderStyle="none" label="main">
      <mx:Button click="onClick(event)" />
    </mx:Canvas>
    

    将转换为:

    new UIComponentDescriptor({
        "type":Canvas,
        "id":mainCanvas,
        "styleFactory":function():void {
            this.borderStyle="none";
        },
        "propertiesFactory":function():Object {
            return({
                "label":"main"
                "childDescriptors":[new UIComponentDescriptor({
                 // button's code in here
                });
                ]
            })
        }
    })
    

    对于那些事件:

    由于您可以在mxml标记中编写click事件,例如btnOnClick()btnOnClick(event),编译器需要执行某些操作以确保事件处理程序接受正确的参数。所以,对于我刚才提到的Button标签, 与键"event"对应的值将如下所示:{"click":"__on_click"}。然后编译器将创建一个新的函数调用__on_click,它可能是这样的:

    public function __on_click(event:MouseEvent):void {
        onClick(event);
    }
    

    显然,在将UIComponentDescriptor转换为.mxml后,您应该替换事件函数。

    对于构造函数:

    .mxml文件不能有构造函数,但编译器会在.as文件中创建一个包含UIComponentDescriptor的文件。构造函数也会为它的变量做一些初始化。转换为.mxml文件时,需要在变量声明后进行初始化。 例如:

    public function MyCanvas(){
        this._documentDescriptor_ = new UIComponentDescriptor({
        //……
        });
        this.myArray = new Array();
    }
    

    你应该把它转换成这个:

    <fx:Script><![CDATA[
    var myArray:Array = new Array();
    ]]></fx:Script>
    

    因此,最重要的工作是将UIComponentDescriptor转换为.mxml标记。这是一个简单的python脚本,可以帮助你做到这一点:

    UIComponentDescriptor2XML