网址: http://www.nshen.net/blog/article.asp?id=467
*找到常见<mx: ...>前缀标签对应的类
安装目录\Adobe\Flex Builder 2\Flex SDK 2\frameworks\mxml-manifest.xml.
*代码ctrl+s以后,想返回以前保存的版本
在你代码上点右键--->Replace with ---> LocalHistory
会显示该文件最近50个保存版本
*清楚显示组件是不是放在正确的container里
在设计模式点这个刷新右边的按钮,图:
*显示图片
loading方式加载图片
<mx:Image source="assets/dairy_milk.jpg" scaleContent="true"/>
把图片compile进swf
<mx:Image source="@Embed('assets/dairy_milk.jpg')" scaleContent="true"/>
用本地xml做model
<mx:Model> 标签 可以把xml文件complie进swf,从而很方便的进行类绑定
<mx:Model id="groceryInventory" source="assets/inventory.xml"/>
inventory.xml文件内容
<groceries>
<catID>1</catID>
<prodName>Milk</prodName>
<unitID>2</unitID>
<cost>1.20</cost>
<listPrice>1.99</listPrice>
<description>Direct from California where cows are happiest</description>
<isOrganic>true</isOrganic>
<isLowFat>true</isLowFat>
<imageName>dairy_milk.jpg</imageName>
</groceries>
然后在main文件里
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="prodHandler(groceryInventory)">
<mx:Script>
<![CDATA[
private function prodHandler(theItems:Object):void
{
//这里可以看到结果了
trace (theItems.prodName);
trace (theItems.cost);
}
]]>
</mx:Script>
* Bindable
http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_8.html
在包定义后,类定义前,加入[Bindable],表示该类所有属性都可以绑定
package valueObjects {
[Bindable]
public class Product {
public var catID:Number;
}
}
*如果绑定一个Object 只有在Object的引用改变的时候才会引发更新
Obejct内部属性改变不会引发更新
*绑定setter 必须有getter ...要成对儿绑定
[Bindable]
public function set shortNames(val:Boolean):void {
...
}
public function get shortNames():Boolean {
...
}
因为当你给shotNames 设置值的时候, flex 会自动调用对应getter 方法来检测 这个值是否改变了
默认情况下如果值改变了,会广播一个PropertyChangeEvent类型的propertyChange 事件.
你也可以自己负责广播这个事件,这样flex就不自动判断值是否改变了
[Bindable(event="changeShortNames")]
public function set shortNames(val:Boolean):void {
...
// Create and dispatch event.
dispatchEvent(new Event("changeShortNames"));
}
// Get method.
public function get shortNames():Boolean {
...
}
// Define private variable.
private var _maxFontSize:Number = 15;
[Bindable(event="maxFontSizeChanged")]
// Define public getter method.
public function get maxFontSize():Number {
return _maxFontSize;
}
// Define public setter method.
public function set maxFontSize(value:Number):void {
if (value <= 30) {
_maxFontSize = value;
} else _maxFontSize = 30;
// Create event object.
var eventObj:Event = new Event("maxFontSizeChanged");
dispatchEvent(eventObj);
}
value Object模式
flex中常见的设计模式,建议可以去看看riawave
package valueObjects {
[Bindable]
public class Product {
public var catID:Number;
public var prodName:String;
public var unitID:Number;
public var cost:Number;
public var listPrice:Number;
public var description:String;
public var isOrganic:Boolean;
public var isLowFat:Boolean;
public var imageName:String;
//可以定义个静态方法用于创建
public static function buildProduct(o:Object):Product{
var p:Product = new Product(o.catID, o.prodName, o.unitID, o.cost, o.listPrice, o
.description, Boolean(o.isOrganic), Boolean(o.isLowFat), o.imageName);
return p;
}
}
}
Event Object
<mx:Application xmlns:mx="http://www.adobe.com/2005/mxml" xmlns="*"
layout="absolute">
<mx:Script>
<![CDATA[
private function myFunction(event:Event):void{
//注意这里
trace (event.currentTarget);
trace (event.type);
}
]]>
</mx:Script>
<mx:Button id="myButton" click="myFunction(event)"/>
</mx:Application>
不同域间交流必须有 crossdomain文件
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
http://www.cnn.com/crossdomain.xml
http://www.flexgrocer.com/crossdomain.xml
HttpService
引用
my_httpservice.lastResult.xxx
与
event.result.xxx
相同
[Bindable]
private var units:ArrayCollection=new ArrayCollection();
<mx:HTTPService id="unitRPC"
url="http://www.flexgrocer.com/units.xml"
result="unitRPCResult(event)"/>
private function unitRPCResult(event:ResultEvent):void{
units=event.result.allUnits.unit;
}
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="unitRPC.send()">
<mx:List id="unitID"
rowCount="4"
dataProvider="{units}"
labelField="unitName"/>
arraycollection里加一个元素
private function catHandler(event:ResultEvent):void{
categories = event.result.catalog.category;
var catObj:Object = new Object();
catObj.name = "All";
catObj.categoryID = 0;
categories.addItemAt(catObj, 0);
catCombo.selectedIndex = 0;
}
实现IEventDispatcher接口,广播事件
用于,当类本身继承其他的类,不能直接继承EventDispatcher时,可以以实现IEventDispatcher接口的方法实现广播
需要实现5个方法
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, weakRef: Boolean = false):void {
_eventDispatcher.addEventListener(type, listener,useCapture, priority, weakRef);
}
public function dispatchEvent(event:Event):Boolean {
return _eventDispatcher.dispatchEvent(event);
}
public function hasEventListener(type:String):Boolean {
return _eventDispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
_eventDispatcher.removeEventListener(type, listener,useCapture);
}
public function willTrigger(type:String):Boolean {
return _eventDispatcher.willTrigger(type);
}
}
继承flash.utils.Proxy类,可以捕获undefined
一定要先导入flash_proxy命名空间
import flash.utils.flash_proxy;
然后覆盖flash_proxy命名空间下的 getProperty方法,可以实现对该类不存在属性访问的捕获
/*捕获到该类不存在的属性,并把这次访问修改成访问xml对应的值*/
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<cs:settings xmlns:cs="http://www.dannypatterson.com/2006/ConfigSettings">
<cs:property id="testOne" value="This is the first test value." />
<cs:property id="testTwo" value="This is the second test value." />
</cs:settings>
flash_proxy override function getProperty(name:*):* {
//找到xml的cs命名空间
var cs:Namespace = _data.namespace("cs");
//将属性值强制为String型
var qname:String = String(name);
return _data.cs::property.(@id == qname).@value;
}
* .客户端与服务器端 valueobject mapping
To allow Flex to map between client-side and server-side value objects,
you must provide some hints to the Flex server about the relationship
between the classes. In Cairngorm, you achieve this by advocating that
each and every instance of a value object class contains the following:
public static var registered:Boolean = Object.registerClass(
"org.nevis.cairngorm.samples.store.vo.ProductVO", ProductVO );
This line ensures that a ProductVO class on the client maps to the Java
class ProductVO.java that resides in the package
org.nevis.cairngorm.samples.store.vo.ProductVO on the server.
* . [Event("xxx")]标签
自定义的组建如果有广播事件,那么请给这个组件广播的事件加到[Event("xxx")]里,这样就是告诉Flex compiler你可以在mxml里使用了这个事件了,例如:
如果你的自定义组件中有
dispatchEvent( new Event(Event.CHANGE) );
那么你应该在你的组里加上这个
<mx:Metadata>
[Event(name="change",type="flash.events.Event")]
</mx:Metadata>
这样你就可以在mxml里这样用了
<你的组件 change="handleCycleChange(event)" />
addChildAt()
addChildAt() specifies the position at which to add the object. If you specify a position that is already occupied, the object at that position, and those at higher positions, move up by 1.
所以以前的遍历mc
for each (var i in mc) {
trace(mc[i]);
}
上边那个枚举mc中所有mc~~~
现在要这么写了
for i=0 i<numChildren i++
getChildAt( i)
ViewStack 的每个子页面第一次显示的时候才会触发creationComplete,而且只会触发一次
* 让Flex 编译出指定大小和帧频的swf
右击项目,选择 Properties 菜单项,在弹出的 Properties for ConjeeAlbum 面板左侧菜单选择 ActionScript Compiler ,在右面的 Additional compiler arguments: 里输入如下命令:
-default-size 800 600 -default-background-color 0xFFFFFF -default-frame-rate 25
也可以在类代码所有 import 语句之后、class 定义之前输入:
[SWF(width="800", height="600", frameRate="25", backgroundColor="#FFFFFF")]
* 调用removeChild之前先检查是否存在
当你试图在displayObject上调用removeChild 删除的child不存在时会出现这样的错误
TypeError: Error #1009: Cannot access a property or method of a null object reference.
为了避免这种情况,判断一下是否存在是必要地
if(MC) if(MC.parent ) MC.parent.removeChild(MC);
*找到常见<mx: ...>前缀标签对应的类
安装目录\Adobe\Flex Builder 2\Flex SDK 2\frameworks\mxml-manifest.xml.
*代码ctrl+s以后,想返回以前保存的版本
在你代码上点右键--->Replace with ---> LocalHistory
会显示该文件最近50个保存版本
*清楚显示组件是不是放在正确的container里
在设计模式点这个刷新右边的按钮,图:
*显示图片
loading方式加载图片
<mx:Image source="assets/dairy_milk.jpg" scaleContent="true"/>
把图片compile进swf
<mx:Image source="@Embed('assets/dairy_milk.jpg')" scaleContent="true"/>
用本地xml做model
<mx:Model> 标签 可以把xml文件complie进swf,从而很方便的进行类绑定
<mx:Model id="groceryInventory" source="assets/inventory.xml"/>
inventory.xml文件内容
<groceries>
<catID>1</catID>
<prodName>Milk</prodName>
<unitID>2</unitID>
<cost>1.20</cost>
<listPrice>1.99</listPrice>
<description>Direct from California where cows are happiest</description>
<isOrganic>true</isOrganic>
<isLowFat>true</isLowFat>
<imageName>dairy_milk.jpg</imageName>
</groceries>
然后在main文件里
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="prodHandler(groceryInventory)">
<mx:Script>
<![CDATA[
private function prodHandler(theItems:Object):void
{
//这里可以看到结果了
trace (theItems.prodName);
trace (theItems.cost);
}
]]>
</mx:Script>
* Bindable
http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_8.html
在包定义后,类定义前,加入[Bindable],表示该类所有属性都可以绑定
package valueObjects {
[Bindable]
public class Product {
public var catID:Number;
}
}
*如果绑定一个Object 只有在Object的引用改变的时候才会引发更新
Obejct内部属性改变不会引发更新
*绑定setter 必须有getter ...要成对儿绑定
[Bindable]
public function set shortNames(val:Boolean):void {
...
}
public function get shortNames():Boolean {
...
}
因为当你给shotNames 设置值的时候, flex 会自动调用对应getter 方法来检测 这个值是否改变了
默认情况下如果值改变了,会广播一个PropertyChangeEvent类型的propertyChange 事件.
你也可以自己负责广播这个事件,这样flex就不自动判断值是否改变了
[Bindable(event="changeShortNames")]
public function set shortNames(val:Boolean):void {
...
// Create and dispatch event.
dispatchEvent(new Event("changeShortNames"));
}
// Get method.
public function get shortNames():Boolean {
...
}
// Define private variable.
private var _maxFontSize:Number = 15;
[Bindable(event="maxFontSizeChanged")]
// Define public getter method.
public function get maxFontSize():Number {
return _maxFontSize;
}
// Define public setter method.
public function set maxFontSize(value:Number):void {
if (value <= 30) {
_maxFontSize = value;
} else _maxFontSize = 30;
// Create event object.
var eventObj:Event = new Event("maxFontSizeChanged");
dispatchEvent(eventObj);
}
value Object模式
flex中常见的设计模式,建议可以去看看riawave
package valueObjects {
[Bindable]
public class Product {
public var catID:Number;
public var prodName:String;
public var unitID:Number;
public var cost:Number;
public var listPrice:Number;
public var description:String;
public var isOrganic:Boolean;
public var isLowFat:Boolean;
public var imageName:String;
//可以定义个静态方法用于创建
public static function buildProduct(o:Object):Product{
var p:Product = new Product(o.catID, o.prodName, o.unitID, o.cost, o.listPrice, o
.description, Boolean(o.isOrganic), Boolean(o.isLowFat), o.imageName);
return p;
}
}
}
Event Object
<mx:Application xmlns:mx="http://www.adobe.com/2005/mxml" xmlns="*"
layout="absolute">
<mx:Script>
<![CDATA[
private function myFunction(event:Event):void{
//注意这里
trace (event.currentTarget);
trace (event.type);
}
]]>
</mx:Script>
<mx:Button id="myButton" click="myFunction(event)"/>
</mx:Application>
不同域间交流必须有 crossdomain文件
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>
http://www.cnn.com/crossdomain.xml
http://www.flexgrocer.com/crossdomain.xml
HttpService
引用
my_httpservice.lastResult.xxx
与
event.result.xxx
相同
[Bindable]
private var units:ArrayCollection=new ArrayCollection();
<mx:HTTPService id="unitRPC"
url="http://www.flexgrocer.com/units.xml"
result="unitRPCResult(event)"/>
private function unitRPCResult(event:ResultEvent):void{
units=event.result.allUnits.unit;
}
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="unitRPC.send()">
<mx:List id="unitID"
rowCount="4"
dataProvider="{units}"
labelField="unitName"/>
arraycollection里加一个元素
private function catHandler(event:ResultEvent):void{
categories = event.result.catalog.category;
var catObj:Object = new Object();
catObj.name = "All";
catObj.categoryID = 0;
categories.addItemAt(catObj, 0);
catCombo.selectedIndex = 0;
}
实现IEventDispatcher接口,广播事件
用于,当类本身继承其他的类,不能直接继承EventDispatcher时,可以以实现IEventDispatcher接口的方法实现广播
需要实现5个方法
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, weakRef: Boolean = false):void {
_eventDispatcher.addEventListener(type, listener,useCapture, priority, weakRef);
}
public function dispatchEvent(event:Event):Boolean {
return _eventDispatcher.dispatchEvent(event);
}
public function hasEventListener(type:String):Boolean {
return _eventDispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
_eventDispatcher.removeEventListener(type, listener,useCapture);
}
public function willTrigger(type:String):Boolean {
return _eventDispatcher.willTrigger(type);
}
}
继承flash.utils.Proxy类,可以捕获undefined
一定要先导入flash_proxy命名空间
import flash.utils.flash_proxy;
然后覆盖flash_proxy命名空间下的 getProperty方法,可以实现对该类不存在属性访问的捕获
/*捕获到该类不存在的属性,并把这次访问修改成访问xml对应的值*/
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<cs:settings xmlns:cs="http://www.dannypatterson.com/2006/ConfigSettings">
<cs:property id="testOne" value="This is the first test value." />
<cs:property id="testTwo" value="This is the second test value." />
</cs:settings>
flash_proxy override function getProperty(name:*):* {
//找到xml的cs命名空间
var cs:Namespace = _data.namespace("cs");
//将属性值强制为String型
var qname:String = String(name);
return _data.cs::property.(@id == qname).@value;
}
* .客户端与服务器端 valueobject mapping
To allow Flex to map between client-side and server-side value objects,
you must provide some hints to the Flex server about the relationship
between the classes. In Cairngorm, you achieve this by advocating that
each and every instance of a value object class contains the following:
public static var registered:Boolean = Object.registerClass(
"org.nevis.cairngorm.samples.store.vo.ProductVO", ProductVO );
This line ensures that a ProductVO class on the client maps to the Java
class ProductVO.java that resides in the package
org.nevis.cairngorm.samples.store.vo.ProductVO on the server.
* . [Event("xxx")]标签
自定义的组建如果有广播事件,那么请给这个组件广播的事件加到[Event("xxx")]里,这样就是告诉Flex compiler你可以在mxml里使用了这个事件了,例如:
如果你的自定义组件中有
dispatchEvent( new Event(Event.CHANGE) );
那么你应该在你的组里加上这个
<mx:Metadata>
[Event(name="change",type="flash.events.Event")]
</mx:Metadata>
这样你就可以在mxml里这样用了
<你的组件 change="handleCycleChange(event)" />
addChildAt()
addChildAt() specifies the position at which to add the object. If you specify a position that is already occupied, the object at that position, and those at higher positions, move up by 1.
所以以前的遍历mc
for each (var i in mc) {
trace(mc[i]);
}
上边那个枚举mc中所有mc~~~
现在要这么写了
for i=0 i<numChildren i++
getChildAt( i)
ViewStack 的每个子页面第一次显示的时候才会触发creationComplete,而且只会触发一次
* 让Flex 编译出指定大小和帧频的swf
右击项目,选择 Properties 菜单项,在弹出的 Properties for ConjeeAlbum 面板左侧菜单选择 ActionScript Compiler ,在右面的 Additional compiler arguments: 里输入如下命令:
-default-size 800 600 -default-background-color 0xFFFFFF -default-frame-rate 25
也可以在类代码所有 import 语句之后、class 定义之前输入:
[SWF(width="800", height="600", frameRate="25", backgroundColor="#FFFFFF")]
* 调用removeChild之前先检查是否存在
当你试图在displayObject上调用removeChild 删除的child不存在时会出现这样的错误
TypeError: Error #1009: Cannot access a property or method of a null object reference.
为了避免这种情况,判断一下是否存在是必要地
if(MC) if(MC.parent ) MC.parent.removeChild(MC);