【问题标题】:Main mxml file and as file - how to exchange data主 mxml 文件和 as 文件 - 如何交换数据
【发布时间】:2013-03-02 12:07:32
【问题描述】:

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

这是代码 主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 的人来说,这可能是一个简单的问题。 对我来说解决这个问题会很有帮助。

【问题讨论】:

    标签: actionscript flex4 flash-builder


    【解决方案1】:

    按照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));
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 2012-03-16
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多