WPF calls Microsoft online translation API to implement translation functions

Source: Internet
Author: User
Tags listsource

It took almost two days to complete this simple translation software (of course, it's just a gimmick !)
Since Microsoft's translation API is referenced, its Title is: MCTranslation.
First, after registering at the Bing Developer Center, you will get your own app id (which will be used during programming). I developed it in VS2010, after creating a new WPF application, right-click the application and click "add service reference ",
REFERENCE The http://api.microsofttranslator.com/V2/Soap.svc (HTTP, AJAX, SOAP three ways to choose their own), click "go", click the service to view the relevant operations and methods, the namespace entered below is named based on your hobbies.
After everything is okay, now we start designing our own pages and programming (with my work attached ):
 

 

 


Two Label controls, one Image, one Button, two comboboxes, and two TextBox.
The following is the CS code:
View Code
1 public partial class Translation: Window
2 {
3 TranslatorService. LanguageServiceClient client = new TranslatorService. LanguageServiceClient ();
4 public Translation ()
5 {
6 InitializeComponent ();
7}
8
9 private void Window_Loaded (object sender, RoutedEventArgs e)
10 {
11 string [] Languagescode = client. GetLanguagesForTranslate ("yours app id"); // obtain the language
12 string [] LanguagesName = client. GetLanguageNames ("yours app id", "zh-CHS", Languagescode); // The obtained language is translated into Chinese.
13
14 ArrayList listSource = new ArrayList ();
15 for (int I = 0; I <Languagescode. Length; I ++)
16 {
17 listSource. Add (new DictionaryEntry (Languagescode [I], LanguagesName [I]);
18}
19
20 ArrayList listPurpose = new ArrayList ();
21 for (int I = 0; I <Languagescode. Length; I ++)
22 {
23 listPurpose. Add (new DictionaryEntry (Languagescode [I], LanguagesName [I]);
24}
25 // Source Language
26 cmbSource. ItemsSource = listSource;
27 cmbSource. DisplayMemberPath = "Value ";
28 cmbSource. SelectedValuePath = "Key ";
29 cmbSource. SelectedValue = "en ";
30
31 // target language
32 cmbPurpose. ItemsSource = listPurpose;
33 cmbPurpose. DisplayMemberPath = "Value ";
34 cmbPurpose. SelectedValuePath = "Key ";
35 cmbPurpose. SelectedValue = "zh-CHS ";
36}
37
38 private void btnChange_Click (object sender, RoutedEventArgs e)
39 {
40 string result = string. Empty;
41 result = client. translate ("yours app id", txtSource. text, cmbSource. selectedValue. toString (), cmbPurpose. selectedValue. toString (), "text/html", "general ");
42 txtPurpose. Text = result;
43}
44
45 private void imgChange_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
46 {
47 object change_Value;
48 BitmapImage urlImgSource = (BitmapImage) this. Resources ["imgSource"];
49 BitmapImage urlImgPurpose = (BitmapImage) this. Resources ["imgPurpose"];
50 if (urlImgSource = imgChange. Source)
51 {
52 // switch Language
53 change_Value = cmbSource. SelectedValue;
54 cmbSource. SelectedValue = cmbPurpose. SelectedValue;
55 cmbPurpose. SelectedValue = change_Value;
56
57 imgChange. Source = urlImgPurpose;
58}
59 else
60 {
61 // language change
62 change_Value = cmbSource. SelectedValue;
63 cmbSource. SelectedValue = cmbPurpose. SelectedValue;
64 cmbPurpose. SelectedValue = change_Value;
65
66 imgChange. Source = urlImgSource;
67}
68}
69}
 
Www.2cto.com
BitmapImage urlImgSource = (BitmapImage) this. Resources ["imgSource"];
BitmapImage urlImgPurpose = (BitmapImage) this. Resources ["imgPurpose"];
Add
Xmlns: imgs = "clr-namespace: System. Windows. Media. Imaging; assembly = PresentationCore"
<Window. Resources>


</Window. Resources>
You can.
PS: This is my first time using WPF. I hope you can give me some advice. The default source language is set to English, and the target language is set to simplified Chinese. by clicking the arrow icon on the left of the translation button, the source language and target language are exchanged, however, the changes to the icon are not fully implemented. It is strange that the image is not switched when you click it for the first time, so you are not familiar with the Source attribute. In general, I feel that developing WPF and WEB is quite different.
 
Author Seabert ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.