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
1 |
using Bing;
|
(3) 撰寫程式
要如何使用 Bing Sharp 來做 翻譯,作者有提供 help 檔案可以下載。而要使用 Bing,就必須要有 AppID,申請的方式請參考 [Windows Mobile]修練筆記(9) - 在智慧型裝置專案、Windows Form 應用程式與 Widget 使用 Bing API 達成 Bing 圖片搜尋。
以下是程式碼
01 |
using System;
|
02 |
using System.Collections.Generic;
|
03 |
using System.ComponentModel;
|
04 |
using System.Data;
|
05 |
using System.Drawing;
|
06 |
using System.Linq;
|
07 |
using System.Text;
|
08 |
using System.Windows.Forms;
|
09 |
10 |
using Bing;
|
11 |
12 |
namespace WindowsFormsBing
|
13 |
{ |
14 |
public partial class Form1 : Form
|
15 |
{
|
16 |
public Form1()
|
17 |
{
|
18 |
InitializeComponent();
|
19 |
20 |
AddItem(cboFrom);
|
21 |
AddItem(cboTo);
|
22 |
}
|
23 |
24 |
string AppId = "B692A148D1624C4E3C1248C8E5DDC209E524D2C4";
|
25 |
26 |
private void btnTranslate_Click(object sender, EventArgs e)
|
27 |
{
|
28 |
string query = txtQuery.Text.Trim();
|
29 |
this.txtResult.Text = String.Empty;
|
30 |
31 |
if (string.IsNullOrEmpty(query))
|
32 |
{
|
33 |
MessageBox.Show("請輸入要翻譯的文字");
|
34 |
return;
|
35 |
}
|
36 |
37 |
if (this.cboFrom.SelectedIndex < 0)
|
38 |
{
|
39 |
MessageBox.Show("請選擇來源語系");
|
40 |
return;
|
41 |
}
|
42 |
43 |
if (this.cboTo.SelectedIndex < 0)
|
44 |
{
|
45 |
MessageBox.Show("請選擇目的語系");
|
46 |
return;
|
47 |
}
|
48 |
49 |
SearchRequest searchRequest = new SearchRequest() { AppId = AppId, Query = query, Market = "en-US" };
|
50 |
51 |
TranslationRequest translationRequest = new TranslationRequest(); // 宣告 TranslationRequest 並實例化
|
52 |
translationRequest.SourceLanguage = cboFrom.Text.ToString(); // 來源語系
|
53 |
translationRequest.TargetLanguage = cboTo.Text.ToString(); // 目的語系
|
54 |
55 |
TranslationResponse response = API.Translation(searchRequest, translationRequest);
|
56 |
57 |
if (response.TranslationResults.Count > 0) // 翻譯回傳值大於0筆時
|
58 |
{
|
59 |
txtResult.Text = response.TranslationResults[0].TranslatedTerm;
|
60 |
}
|
61 |
}
|
62 |
63 |
public void AddItem(ComboBox cbo)
|
64 |
{
|
65 |
cbo.Items.Add("Ar"); // Arabic
|
66 |
cbo.Items.Add("zh-CHS");// Simplified Chinese
|
67 |
cbo.Items.Add("zh-CHT");// Traditional Chinese
|
68 |
cbo.Items.Add("Nl"); // Dutch
|
69 |
cbo.Items.Add("En"); // English
|
70 |
cbo.Items.Add("Fr"); // French
|
71 |
cbo.Items.Add("De"); // German
|
72 |
cbo.Items.Add("It"); // Italian
|
73 |
cbo.Items.Add("Ja"); // Japanese
|
74 |
cbo.Items.Add("Ko"); // Korean
|
75 |
cbo.Items.Add("Pl"); // Polish
|
76 |
cbo.Items.Add("Pt"); // Portuguese
|
77 |
cbo.Items.Add("Ru"); // Russian
|
78 |
cbo.Items.Add("Es"); //"Spanish
|
79 |
}
|
80 |
}
|
81 |
} |
(4) 執行結果
3. 檔案下載
4. 附註
會使用 Bing Sharp 的原因是因為 Plurk 網友詢問這是微軟新出的程式語言嗎? 而且看到大陸網站上有關於 Bing Sharp 的介紹,不過總覺得這應該不是微軟官方的東西,反正好用的話,大家就會使用了
原文:http://www.dotblogs.com.tw/chou/archive/2009/08/11/10002.aspx