完成后更改状态

发布于 2024-10-08 02:32:14 字数 3359 浏览 4 评论 0原文

使用 Flash Builder 4(带有 4.1 flex)。 尝试在我的函数完成运行后创建状态更改,这会将屏幕更改为具有“完成”按钮,该按钮将关闭应用程序。我如何告诉 Air 应用程序执行此操作?
我发现这个: protected function setState(newState:String):void for AS3 but won't work on flex 4. 在文件数组完成后,它应该将状态更改为 Finished。

        private function initiate_download(event:MouseEvent):void
        {
            var filearray:Array = new Array();
            filearray[0]="00.jpg";
            filearray[1]="01.jpg";
            filearray[2]="02.jpg";
            for (var i:uint; i < filearray.length; i++) {
                var remoteURL = "http://domain/" + filearray[i];
                var localURL = "C:/dir/" + filearray[i];
                downloadFile(remoteURL, localURL);
            }


            function downloadFile(url, filename) {
                // Create the stream for the data request
                var urlStream = new URLStream();

                // Used to initiate request for remote file
                var request = new URLRequest(url);

                // Create file stream
                var fileStream = new FileStream();

                // Create a reference to a location on disk
                var file = File.desktopDirectory.resolvePath(filename);

                // Called as download progresses
                var writeFile = function()
                {
                    // Write to file
                    if (urlStream.bytesAvailable > 51200)
                    {
                        var dataBuffer = new ByteArray();
                        urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                        fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                    }
                    return true;
                }

                // Called when download completes
                var finishWriteFile = function()
                {
                    // Write to file
                    if(urlStream.bytesAvailable > 0)
                    {
                        var dataBuffer = new ByteArray();
                        urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                        fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                    }

                    // Close streams
                    fileStream.close();
                    urlStream.close();

                    return true;
                }

                // Initiate download
                fileStream.openAsync(file, FileMode.WRITE);
                urlStream.load(request);

                // Add event listeners
                urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
            }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:states>
    <s:State name="Start"/>
    <s:State name="Finished"/>
</s:states>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" includeIn="Start" label="Update" id="download" click="initiate_download(event)" color="#FFFFFF" fontSize="30"/>
<s:Button x="131" y="85" includeIn="Finished" label="Finished" id="closer" click="close_window(event)" color="#FFFFFF" fontSize="30"/>

Using Flash Builder 4 (with 4.1 flex).
Trying to create a state change after my function has finished running that will change the screen to have a "finished" button that will close the application. How can I tell the air app to do this?
I found this: protected function setState(newState:String):void for AS3 but won't work on flex 4. It should change state to Finished after the array of files is done.

        private function initiate_download(event:MouseEvent):void
        {
            var filearray:Array = new Array();
            filearray[0]="00.jpg";
            filearray[1]="01.jpg";
            filearray[2]="02.jpg";
            for (var i:uint; i < filearray.length; i++) {
                var remoteURL = "http://domain/" + filearray[i];
                var localURL = "C:/dir/" + filearray[i];
                downloadFile(remoteURL, localURL);
            }


            function downloadFile(url, filename) {
                // Create the stream for the data request
                var urlStream = new URLStream();

                // Used to initiate request for remote file
                var request = new URLRequest(url);

                // Create file stream
                var fileStream = new FileStream();

                // Create a reference to a location on disk
                var file = File.desktopDirectory.resolvePath(filename);

                // Called as download progresses
                var writeFile = function()
                {
                    // Write to file
                    if (urlStream.bytesAvailable > 51200)
                    {
                        var dataBuffer = new ByteArray();
                        urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                        fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                    }
                    return true;
                }

                // Called when download completes
                var finishWriteFile = function()
                {
                    // Write to file
                    if(urlStream.bytesAvailable > 0)
                    {
                        var dataBuffer = new ByteArray();
                        urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
                        fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
                    }

                    // Close streams
                    fileStream.close();
                    urlStream.close();

                    return true;
                }

                // Initiate download
                fileStream.openAsync(file, FileMode.WRITE);
                urlStream.load(request);

                // Add event listeners
                urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
            }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:states>
    <s:State name="Start"/>
    <s:State name="Finished"/>
</s:states>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" includeIn="Start" label="Update" id="download" click="initiate_download(event)" color="#FFFFFF" fontSize="30"/>
<s:Button x="131" y="85" includeIn="Finished" label="Finished" id="closer" click="close_window(event)" color="#FFFFFF" fontSize="30"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2024-10-15 02:32:14

关闭 fileStream 和 urlStream 后,您可以更改状态

this.currentState = "Finished"

,并且关闭按钮现在应该可见。

after you close the fileStream and urlStream you can change state doing

this.currentState = "Finished"

and the close button should be visible now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文