主要的mxml文件和文件 - 如何交换数据

时间:2013-03-14 08:20:53

标签: actionscript flex4 flash-builder

我有一个主mxml文件和一个phpconnect.as文件。当我从mxml文件调用phpconnect-function连接工作正常时,控制台中的跟踪工作正常。但是将phpconnect.as文件的结果恢复到mxml文件的最佳方法是什么。

这是代码 main mxml Test02HomeView.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
    <fx:Declarations>
        <!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[

            import comps.PHPConnect;

            protected function phpConnect_btn_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                var phpconnect:PHPConnect = new PHPConnect();
                phpconnect.phpconnect();
            }

        ]]>
    </fx:Script>

    <s:TextArea id="log_txt" x="10" y="128" height="104"/>
    <s:Button id="phpConnect_btn" x="180" y="256" width="130" label="PHPConnect" click="phpConnect_btn_clickHandler(event)"/>

</s:View>

这是来自phpconnect.as的代码

package comps
{
    import flash.events.Event;
    import flash.events.HTTPStatusEvent;
    import flash.events.IEventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class PHPConnect
    {
        public function PHPConnect()
        {
        }


        public function phpconnect():void
        {
            // TODO Auto-generated method stub
            var loader:URLLoader = new URLLoader();
            configureListeners(loader);

            var request:URLRequest = new URLRequest("http://localhost/file.php");
            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested data.");
            }

        }

        private function configureListeners(dispatcher:IEventDispatcher):void
        {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            trace("completeHandler: " + loader.data);
            // log_txt.text = loader.data;
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }


    }
}

想想使用actionscript和FlashBuilder的人,这可能是一个简单的问题。 对我来说,解决这个问题会有很大的帮助。

1 个答案:

答案 0 :(得分:0)

按照here说明创建自定义事件类,并在PHPConnect类成功加载可作为事件属性传递的数据时调度它。您现在可以在主mxml类中订阅该事件,并通过事件对象访问加载的数据。

或者/另外,您可以将数据设置为PHPConnect类的公共属性的值,并通过实例而不是通过事件对象访问它。

<强> Test02HomeView.mxml:

protected function phpConnect_btn_clickHandler(event:MouseEvent):void
{
    // TODO Auto-generated method stub
    var phpconnect:PHPConnect = new PHPConnect();
    phpconnect.addEventListener(CustomEvent.LOADED, this.dataLoadedHandler);
    phpconnect.phpconnect();
}

private function dataLoadedHandler(event:CustomEvent):void
{
    trace("Data loaded:", event.data);

    // Or, assuming instance of phpconnect is in scope and has public data property
    //trace("Data loaded:", phpconnect.data);
}

<强> PhpConnect.as:

private function completeHandler(event:Event):void {
    //var loader:URLLoader = URLLoader(event.target);
    trace("completeHandler: " + data);
    // log_txt.text = loader.data;

    // Don't need to create a new instance of loader, just cast the target property
    // Now dispatch a custom event with the data as the payload
    dispatchEvent(new CustomEvent(CustomEvent.LOADED, URLLoader(event.target).data));
}