第一个 AIR windows程序

因为是初次写一个AIR,所以很多知识点不是很清楚。只是从网上找了些资料。

下面开始:

1、在 Flex 3中新建一个 Flex Project ,Application Type 选 Desktop Application

image

其它,默认。

2、在 mxml 的 design 模式下放一个button,然后转入  Source 模式,你会看到

< ? x m l   version = " 1.0 "   encoding = " utf-8 " ?>
<mx:WindowedApplication xmlns:mx="
http://www.adobe.com/2006/mxml" layout="absolute" height="308">

    <mx:Button x="68" y="39" click="btnClick(event)" label="Button"/>
</mx:WindowedApplication>

这样的xml文本。

你需要在其中添加如下部分

    <mx:Script>
    <![CDATA[
        import goldli.XmlManager.XmlManager;
        private function createXml():void{
            var xmlFile:String="e://123.xml";//注意转义字符
            var xm:XmlManager=new XmlManager(xmlFile);
            var xmlDoc:XML=
                            <ImagesLibrary>
                            <!--注释-->
                            <Images>
                                <Image>
                                    <name />
                                    <time />
                                    <description />
                                    <author />
                                    <smallImg />
                                    <bigImg />
                                </Image>
                            </Images>
                            </ImagesLibrary>
            xm.create(xmlDoc);
        }
        private function btnClick(e:MouseEvent):void{
            createXml();   
        }
    ]]>
    </mx:Script>

标签的含意就不说了。其中 CDATA 部分就是主要的代码部分,写法就是写 as 代码;

3、以上部分中还有一个  goldli.XmlManager.XmlManager,这是我写的一个 flex library.其代码如下:

package goldli.XmlManager
{
    import flash.filesystem.*;
    public class XmlManager
    {
        private var _xmlFile:String;
        private var _fileStream:FileStream;
        private    var _file:File;
        public function XmlManager(xmlFile:String){
            _xmlFile=xmlFile;   
        }
        public function open():XML{
            _fileStream=new FileStream();
            _file=new File(_xmlFile);
            if (!checkFile(_file)){
                return null;
            }   
            try
            {   
                _fileStream.open(_file,FileMode.READ);
                return _fileStream.readUTFBytes(_fileStream.bytesAvailable) as XML;
            }
            catch(error:Error)
            {
                throw "读取文件时出错. "+error.message;
            }
            return null;
        }
        public function save(data:XML):void{
            _fileStream=new FileStream();
            _file=new File(_xmlFile);
            try
            {   
                _fileStream.open(_file,FileMode.UPDATE);
                _fileStream.writeUTFBytes(data.toString());
            }
            catch(error:Error)
            {
                throw "写入文件时出错. "+error.message;
            }
        }
        public function create(data:XML):void{
            save(data);   
        }
        private function checkFile(fileName:File):Boolean{
            if (!fileName.exists){
                throw "文件不存在! "+fileName.url;
                return false;
            }   
            else
                return true;
        }
    }
}

4、关于调试,直接在flex里按F11就行。只要你设了断点,ide就会跟踪到指定的位置

5、如果你也是把代码部分写到一个library里,那你需要在工程属性里的 flex builder path / library path 使用Add SWC的方式,把它引用过来

6、运行一下,就会在 e:/盘下生成 123.xml

7、不对之处请指正。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值