Feeds:
文章
留言

Archive for 一月 26th, 2007

[Flex] 透過程式驅動特效

 Chui-Wen Chiu(Arick)
MSN SpaceGoogle DocGoogle Blog
2007.1.26

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml&quot; initialize="initMe()">
 
    <mx:Script>
    <![CDATA[
      import flash.utils.Timer;
      import flash.events.TimerEvent;                  
      public var myTimer:Timer;
      
      public function initMe():void {     
        glowThis.target = makeMeGlow; // 套用特效到 UIComponent
        glowThis.end(); // 中斷目前正在播放的特效
        glowThis.play(); // 開始播放特效
      }
                 
    ]]>
    </mx:Script>
    <mx:Sequence id="glowThis" repeatCount="0"> 
        <mx:Glow id="glowImage" duration="1000"
            alphaFrom="1.0" alphaTo="0.3"
            blurXFrom="0.0" blurXTo="50.0"
            blurYFrom="0.0" blurYTo="50.0"
            color="0xFF0000"/>
          
        <mx:Glow id="unglowImage" duration="1000"
            alphaFrom="0.3" alphaTo="1.0"
            blurXFrom="50.0" blurXTo="0.0"
            blurYFrom="50.0" blurYTo="0.0"
            color="0xFF0000"/>      
    </mx:Sequence>     
    <mx:Button id="makeMeGlow" label="Look Ma’, I’m glowing!"/>
   
</mx:Application>

參考資料:
[1] Programmatic Control of an Effect
[2] mx.effects.Sequence (Flex™ 2 Language Reference)
[3]
mx.effects.Glow (Flex™ 2 Language Reference)

Read Full Post »

 Chui-Wen Chiu(Arick)
MSN SpaceGoogle DocGoogle Blog
2007.1.26

1. 要將一個類別序列化,只需要透過 [Serializable] Attribute 即可

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerializeTest {
    class Program {
        static void Main(string[] args) {
            Byte[] bs;
            BinaryFormatter bf = new BinaryFormatter();
            using (MemoryStream inMS = new MemoryStream()) {               
                bf.Serialize(inMS, new ShoppingCartItem(1, 2, 3));

                bs = new Byte[inMS.Length];
                inMS.Seek(0, SeekOrigin.Begin);  
                inMS.Read(bs, 0, (int)inMS.Length);  
            }

            using (MemoryStream outMS = new MemoryStream(bs)){
                ShoppingCartItem sc = bf.Deserialize(outMS) as ShoppingCartItem;
                if (sc != null) {
                    Console.WriteLine(" productId={0}n price={1}n quantity={2}n total={3}n", sc.productId, sc.price, sc.quantity, sc.total);
                    /**
                     *  Output:
                     *  productId=1
                     *  price=2
                     *  quantity=3
                     *  total=6
                     */
                } else {
                    Console.WriteLine("Serialize Fail");
                }
            }

            Console.ReadKey();
        }
    }

    [Serializable]
    class ShoppingCartItem {

        public int productId;
        public decimal price;
        public int quantity;
        public decimal total;

        public ShoppingCartItem(int _productID, decimal _price, int _quantity) {
            productId = _productID;
            price = _price;
            quantity = _quantity;
            total = price * quantity;
        }

    }
}

2. 如何將特定資料成員不進行序列化,在成員上面使用[NonSerialized]

  /**
   *  Output:
   *  productId=1
   *  price=2
   *  quantity=3
   *  total=0
    */

    [Serializable]
    class ShoppingCartItem {

        public int productId;
        public decimal price;
        public int quantity;
        [NonSerialized]
        public decimal total;

        public ShoppingCartItem(int _productID, decimal _price, int _quantity) {
            productId = _productID;
            price = _price;
            quantity = _quantity;
            total = price * quantity;
        }

    }

3. 如果想要讓反序列化之後自動進行一些動作,可以在類別實作 IDeserializationCallback 介面

  /**
   *  Output:
   *  productId=1
   *  price=2
   *  quantity=3
   *  total=6
    */

    [Serializable]
    class ShoppingCartItem : IDeserializationCallback{

        public int productId;
        public decimal price;
        public int quantity;
        [NonSerialized]
        public decimal total;

        public ShoppingCartItem(int _productID, decimal _price, int _quantity) {
            productId = _productID;
            price = _price;
            quantity = _quantity;
            total = price * quantity;
        }

        void IDeserializationCallback.OnDeserialization(Object sender) {
            total = price * quantity;
        }
    }

4. 如果序列化物件有進行資料成員的更動,導致類別前後版本不一致時,可以在變動的資料成員上使用 [OptionalField] ,如果要初始化此欄位資料,可以實作 IDeserializationCallback 介面

  /**
   *  Output:
   *  productId=1
   *  price=2
   *  quantity=3
   *  total=6
    */

    [Serializable]
    class ShoppingCartItem : IDeserializationCallback{

        public int productId;
        public decimal price;
        public int quantity;
        [NonSerialized]
        public decimal total;

        [OptionalField]
          public bool taxable;

        public ShoppingCartItem(int _productID, decimal _price, int _quantity) {
            productId = _productID;
            price = _price;
            quantity = _quantity;
            total = price * quantity;
        }

        void IDeserializationCallback.OnDeserialization(Object sender) {
            total = price * quantity;
        }
    }

5. 實務上要達到類別版本相容的一些準則
    5.1 不要移除任何已經被序列化的欄位
    5.2 假如舊版類別欄位沒有套用 NonSerializedAttribute  Attribute,新版的資料欄位也不要套用此 Attribute
    5.3 不要變更已經序列化的欄位名稱
    5.4 新增一個序列化欄位,記得加上 OptionalFieldAttribute  Attribute
    5.5 假如移除欄位上的 NonSerializedAttribute ,記得加上 OptionalFieldAttribute  Attribute
    5.6 對於所有 OptionalFieldAttribute  欄位透過 IDeserializationCallback 進行初始化

參考資料:
[1] How to Serialize and Deserialize custom classes
[2] SerializableAttribute Class (System)
[3] NonSerializedAttribute Class (System)
[4] OptionalFieldAttribute Class (System.Runtime.Serialization)
[5] IDeserializationCallback Interface (System.Runtime.Serialization)

Read Full Post »