2008/10/17

Flex 檔案下載

private var fr:FileReference = null; //注意,要寫在外面,否則會被記憶體回收掉,下載時會找不到來源,按存檔時會無作用。
private function downloadClipboard(event:MouseEvent):void
{
var clip:Cls_Clipboard = this.selectedClipboard;
if (clip != null) {
fr = new FileReference()
fr.addEventListener(Event.COMPLETE, function():void {
fr = null; //載入完成時,才設成 null 等待記憶體回收。
trace('fr set null');
});
fr.download(new URLRequest(clip.ClipboardPath_Fix), getDownloadFilename(clip.ClipboardPath_Fix, "clipboard-"));
} else {
DiijaUtils.popMsg('請選擇一張剪影才能下載');
}
}

/**
* 回傳如下格式 clipboard-20081017150538.png
*/
private function getDownloadFilename(url:String, prefix:String="clipboard-"):String
{
var formater:DateFormatter = new DateFormatter();
formater.formatString = "YYYYMMDDHHNNSS"; //日期格式請參考 http://livedocs.adobe.com/flex/3/langref/mx/formatters/DateFormatter.html

var split:Array = url.split('.');
return prefix + formater.format(new Date()) + '.' + split[split.length - 1];
}

2008/10/15

如何讓 SWFLoader 與 被載入的程式作溝通

如果使用 SWFLoader 要跟其內部 程式 傳遞參數時,
只要被載入的程式有開放介面提供外部呼叫,
就可以透過 System Manager 來呼叫。
例如:
被載入的程式(SWFLoaded)開放的介面:public function setVarOne(newText:String):void
主程式範例:


private function onCompleteAppLoader(oEvent:Event):void
{
var smAppLoaded:SystemManager = SystemManager(oEvent.target.content);
smAppLoaded.addEventListener(FlexEvent.APPLICATION_COMPLETE, onCurrentApplicationComplete);
}

private function onCurrentApplicationComplete(oEvent:Event):void
{
_appLoaded = Application(oEvent.target.application);
SWFLoaded(_appLoaded).setVarOne("This value set from main app!");
}

參考連結:http://www.cflex.net/showFileDetails.cfm?ObjectID=690

2008/10/13

Flex 取得外部參數

假設你使用Flex 製作出一顆 SWF檔案

需要讀出外部參數

譬如

http://www.sample.com.tw/Main.swf?UserId=Lucifer


因為程式需求需要把UserId讀進來

你可以透過

Application.application.parameters["UserId"];

這方法讀進來

很溫馨吧 !!

2008/10/08

Flex Color Picker 範例

        <mx:ColorPicker id="colorPicker" change="onColorPickerChange(event)" enter="onColorPickerEnter(event)"/>

        /** 選取顏色觸發 **/
        private function onColorPickerChange(event:ColorPickerEvent):void {
            var color:uint = event.currentTarget.selectedItem as uint;
            var colorHex:String = StringUtils.intToHexColor(color);           
            this.setStyle("backgroundColor", colorHex);
        }
        /** 輸入顏色值按 Enter 觸發 **/
        private function onColorPickerEnter(event:ColorPickerEvent):void {
            var colorHex:String = StringUtils.intToHexColor(event.color);
            this.setStyle("backgroundColor", colorHex); 
        }

        /** 將 int 轉為 #FFFFFF 顏色字串格式 **/
        public static function intToHexColor(value:uint):String
        {
            var mask:String = '000000';
            var str:String = mask + value.toString(16).toUpperCase();
            return '#' + str.substr(str.length - 6);
        }

2008/09/18

TabNavigation 標籤改變事件







private function onChange(IndexChangeEvent):void
{
//改變的目標
var child:DisplayObject = event.relatedObject; #相當於 tabnav.getChildAt(event.newIndex);
}