1. 簡介
在之前,曾發過此篇文章,[Windows Mobile]修練筆記(9) - 在智慧型裝置專案、Windows Form 應用程式與 Widget 使用 Bing API 達成 Bing 圖片搜尋,文章中,我們必須對 Bing API 所回傳的 XML、JSON 或 Soap 做處理,而 Bing Sharp 的出現,就省去了這道手續。
Bing Sharp 是一個包含 Bing API 2.x XML 請求的 C# API,提供了與 Bing API 互動的物件模型,使用了 Bing Sharp,則不必去處理 XML、JSON、Soap。
*註 : Bing Sharp 作者為 Joseph Guadagno,是 Micosoft Visual C# MVP
2. 使用 Bing Sharp
(1) 下載
下載網址
(2) 加入參考
在程式中 using
(3) 撰寫程式
要如何使用 Bing Sharp 來做 翻譯,作者有提供 help 檔案可以下載。而要使用 Bing,就必須要有 AppID,申請的方式請參考 [Windows Mobile]修練筆記(9) - 在智慧型裝置專案、Windows Form 應用程式與 Widget 使用 Bing API 達成 Bing 圖片搜尋。
以下是程式碼
02 | using System.Collections.Generic; |
03 | using System.ComponentModel; |
08 | using System.Windows.Forms; |
12 | namespace WindowsFormsBing |
14 | public partial class Form1 : Form |
18 | InitializeComponent(); |
24 | string AppId = "B692A148D1624C4E3C1248C8E5DDC209E524D2C4" ; |
26 | private void btnTranslate_Click( object sender, EventArgs e) |
28 | string query = txtQuery.Text.Trim(); |
29 | this .txtResult.Text = String.Empty; |
31 | if ( string .IsNullOrEmpty(query)) |
33 | MessageBox.Show( "請輸入要翻譯的文字" ); |
37 | if ( this .cboFrom.SelectedIndex < 0) |
39 | MessageBox.Show( "請選擇來源語系" ); |
43 | if ( this .cboTo.SelectedIndex < 0) |
45 | MessageBox.Show( "請選擇目的語系" ); |
49 | SearchRequest searchRequest = new SearchRequest() { AppId = AppId, Query = query, Market = "en-US" }; |
51 | TranslationRequest translationRequest = new TranslationRequest(); |
52 | translationRequest.SourceLanguage = cboFrom.Text.ToString(); |
53 | translationRequest.TargetLanguage = cboTo.Text.ToString(); |
55 | TranslationResponse response = API.Translation(searchRequest, translationRequest); |
57 | if (response.TranslationResults.Count > 0) |
59 | txtResult.Text = response.TranslationResults[0].TranslatedTerm; |
63 | public void AddItem(ComboBox cbo) |
66 | cbo.Items.Add( "zh-CHS" ); |
67 | cbo.Items.Add( "zh-CHT" ); |
(4) 執行結果
3. 檔案下載
WindowsFormsBing.rar
4. 附註
會使用 Bing Sharp 的原因是因為 Plurk 網友詢問這是微軟新出的程式語言嗎? 而且看到大陸網站上有關於 Bing Sharp 的介紹,不過總覺得這應該不是微軟官方的東西,反正好用的話,大家就會使用了
原文:http://www.dotblogs.com.tw/chou/archive/2009/08/11/10002.aspx