顯示具有 Windows Phone 標籤的文章。 顯示所有文章
顯示具有 Windows Phone 標籤的文章。 顯示所有文章

2012年3月24日 星期六

在 Windows Phone 上讀取 BIG5 網頁

SNAGHTML2b39c0e

Windows Phone SDK 是不支援 BIG5 編碼的,它只支援三種編碼。

BigEndianUnicode、Unicode、UTF8

image 

 

因此若要讀取 BIG5 的網頁,就必須自行將 BIG5 轉換為 Unicode,實作的主要重點在於:

  1. 取得 BIG5 –> Unicode 轉換表。(BIG5.TXT)
  2. 將轉換表改用 Dictionary 型態儲存。
  3. 讀取網頁時,用 stream,不要用 WebClient!

使用 WebClient 來讀取網頁,得到的並非是 raw data,而是已被 default encoding 轉換過的 data。更進一步解釋,就是使用了 UTF8 –> Unicode 轉換表來轉換 BIG5 的資料,這會導致資料整個變成不可用的亂碼。

 

實作

將 BIG5.TXT 加入專案

image 

 

在 ContentPanel 加入一個 textBlock

image

 

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
 
using System.IO;
using System.Globalization;
using System.Diagnostics;
using System.Text;
 
namespace PhoneApp2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // async http
        delegate void DownDelegate(string content);
        DownDelegate downDelegate;
 
        // Big5 to Unicode mapping table
        private static Dictionary<int, int> mBIG5_Unicode_MAP = new Dictionary<int, int>();
 
        // 建構函式
        public MainPage()
        {
            InitializeComponent();
            createBig5ToUnicodeDictionary();
            readBig5WebPage();
        }
 
        private void setConent(string content)
        {
            textBlock1.Text = content;
        }
 
        private void createBig5ToUnicodeDictionary()
        {
            var resource = Application.GetResourceStream(new Uri("BIG5.TXT", UriKind.Relative));
            StreamReader sr = new StreamReader(resource.Stream);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                // 忽略註解
                if (line.StartsWith("#")) continue;
                string[] lTokens = line.Split(new char[] {'\t'});
                mBIG5_Unicode_MAP.Add(hexToInt(lTokens[0].Substring(2)), hexToInt(lTokens[1].Substring(2)));
            }
        }
 
        private void readBig5WebPage()
        {
            textBlock1.Text = "讀取中...";
            string url = "http://www.businessweekly.com.tw/feednews.php";
            downDelegate = setConent;
            System.Net.WebRequest request = HttpWebRequest.Create(url);
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request);
        }
 
        private void ResponseCallback(IAsyncResult result)
        {
            HttpWebRequest request = (HttpWebRequest)result.AsyncState;
            WebResponse response = request.EndGetResponse(result);
            Stream s = response.GetResponseStream();
            Dispatcher.BeginInvoke(downDelegate, big5ToUnicode(s).ToString());
        }
 
        private StringBuilder big5ToUnicode(Stream s)
        {
            StringBuilder lSB = new StringBuilder();
            byte[] big5Buffer = new byte[2];
            int input;
            while ((input = s.ReadByte()) != -1)
            {
                if (input > 0x81 && big5Buffer[0] == 0)
                {
                    big5Buffer[0] = (byte)input;
                }
                else if (big5Buffer[0] != 0)
                {
                    big5Buffer[1] = (byte)input;
                    int Big5Char = (big5Buffer[0] << 8) + big5Buffer[1];
                    try
                    {
                        int UTF8Char = mBIG5_Unicode_MAP[Big5Char];
                        lSB.Append((char)UTF8Char);
                    }
                    catch (Exception)
                    {
                        lSB.Append((char)mBIG5_Unicode_MAP[0xA148]);
                    }
 
                    big5Buffer = new byte[2];
                }
                else
                {
                    lSB.Append((char)input);
                }
            }
            return lSB;
        }
 
        private int hexToInt(string hexString)
        {
            return int.Parse(hexString, NumberStyles.HexNumber);
        }
    }
}

 

範例程式碼下載

 

參考連結

2011年9月25日 星期日

Windows Phone 一行程式都不用寫的氣泡遊戲

SNAGHTMLd07ace

事前準備

首先必須先安裝 Windows Phone SDK,安裝方式請直接到微軟的官網下載,並隨著步驟安裝,這邊就不特別一步一步講了。(沒特別需求就下一步到底就對了)

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13890

 

假設已經安裝好了,那麼…

打開 Microsoft Expression Blend 4

image

 

然後新建一個專案

image

 

我們選擇 Windows Phone Application 將專案名稱取名叫 WOW

image

 

打開後會長這樣

image

 

先將改改標題,讓應用程式有名有份

image

 

接著點選左上方的 Assets

image

 

Control –> All –> Image 找到 Image

image

 

將它拖曳到你的主畫面上,可以看到左下方多了一個元素,畫面上也多了一個方框

image

 

接著我們在畫面上點選方框,在右方的 Source 選張圖片給它

image

 

然後回到左上角的 Behaviors ,找到 PlaySoundAction 以及 RemoveElementAction

image

 

PlaySoundAction 以及 RemoveElementAction 拖曳到主畫面的圖片上,就會看到左下角的 Image 底下多了剛剛幫它添加的 Behaviors

image

 

點選 PlaySoundAction ,到右方進行編輯,選個適合的 mp3 給他,記得將音量調到1,避免以為沒有聲音。(這邊可以提供我個人錄製的 wow,沒有版權問題喔!)

image

 

接著存檔,然後 Run Project (其實按下 F5 也可以)

image

 

然後就可以把它點破,並發出你選擇的聲音檔。(剩下就自己多複製幾個泡泡,然後擺到你想要的位置囉!)

SNAGHTMLcdc0b3