Windows Phone 7调用必应翻译服务
这讲是介绍如何在Windows Phone上调用必应翻译服务。想必大家也知道必应为我们提供了很多服务,诸如搜索、地图、翻译等。前面已经介绍了如何在Windows Phone上调用必应的地图服务,今天调用必应的翻译服务,在它的服务中包含36种以上语言,包括简体中文、繁体中文等。下面是一个快照。有兴趣的朋友赶快行动吧!
效果图如下
起始界面
没有选择语言时弹出提示框
没有输入数据时弹出提示框
翻译完成后界面
好啦!开始吧.
Step 1:创建语言类Language.cs
每种语言都有两个属性,Name,Code。如上俩图中文的Name是chinese simplified,Code是zh-CHS.
{
public string Code { get; set; }
public string Name { get; set; }
}
Step 2:用户接口
我们定义了一些控件,并绑定Language的Name属性,来显示数据。
<StackPanel Orientation="Vertical">
<TextBlock FontSize="30">源语言:</TextBlock>
<ListBox x:Name="lbxFrom" Height="160" Margin="0,10,0,10" Width="440" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontSize="30"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Name="txtOrign" Text="" FontSize="25" TextWrapping="Wrap" Height="160" HorizontalAlignment="Left" Width="440"/>
<TextBlock FontSize="30">目标语言:</TextBlock>
<ListBox x:Name="lbxTo" Height="160" Margin="0,10,0,10" Width="440" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontSize="30"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="btnTranslate" Content="翻译"
Click="btnTranslate_Click" />
<TextBox Name="txtTarget" IsReadOnly="True" FontSize="30" TextWrapping="Wrap" Height="160" HorizontalAlignment="Left" Width="440"/>
</StackPanel>
</ScrollViewer>
</UserControl>
在MainPage.cs文件中定义三个List<T>,来保存数据。
private List<string> codes;
private List<string> names;
private List<Language> languages;
Step 3:使用Bing SOAP 服务
添加服务引用,地址为:http://api.microsofttranslator.com/V2/Soap.svc,LanguageServiceClient是一个Proxy,提供了一些调用服务的方法。下面我们定义一个对象
private BTS.LanguageServiceClient proxy;
要使用必应的翻译服务我们首先必须获取一个APP_ID,到http://cn.bing.com/developers/createapp.aspx网站,输入信息来获取。我注册的ID也可以使用,请看下图。
接下来我们注册事件
proxy.GetLanguagesForTranslateCompleted += new EventHandler<BTS.GetLanguagesForTranslateCompletedEventArgs>(proxy_GetLanguagesForTranslateCompleted);
proxy.GetLanguageNamesCompleted += new EventHandler<BTS.GetLanguageNamesCompletedEventArgs>(proxy_GetLanguageNamesCompleted);
proxy.TranslateCompleted += new EventHandler<BTS.TranslateCompletedEventArgs>(proxy_TranslateCompleted);
其中GetLanguagesForTranslateCompleted事件有一个GetLanguagesForTranslateCompletedEventArgs类型的参数。包含一些从服务中得到的数据,如Language的Code值。
GetLanguageNamesCompleted事件有一个GetLanguageNamesCompletedEventArgs类型的参数,包含Language的Name值。
TranslateCompleted事件将我们输入原语言的数据翻译为目标语言的数据。
{
proxy.GetLanguagesForTranslateCompleted += new EventHandler<BTS.GetLanguagesForTranslateCompletedEventArgs>(proxy_GetLanguagesForTranslateCompleted);
proxy.GetLanguageNamesCompleted += new EventHandler<BTS.GetLanguageNamesCompletedEventArgs>(proxy_GetLanguageNamesCompleted);
proxy.TranslateCompleted += new EventHandler<BTS.TranslateCompletedEventArgs>(proxy_TranslateCompleted);
proxy.GetLanguagesForTranslateAsync(APP_ID);
}
public void proxy_GetLanguagesForTranslateCompleted(object sender, BTS.GetLanguagesForTranslateCompletedEventArgs e)
{
codes = e.Result.ToList();
proxy.GetLanguageNamesAsync(APP_ID, "en", e.Result);
}
public void proxy_GetLanguageNamesCompleted(object sender, BTS.GetLanguageNamesCompletedEventArgs e)
{
names = e.Result.ToList();
loadLanguages();
}
public void proxy_TranslateCompleted(object sender, BTS.TranslateCompletedEventArgs e)
{
txtTarget.Text = e.Result;
}
现在给ListBox对象绑定数据
{
for (int index = 0; index < codes.Count; index++)
{
languages.Add(new Language
{
Code = codes[index],
Name = names[index]
});
}
lbxFrom.ItemsSource = languages;
lbxTo.ItemsSource = languages;
}
定义Translate()方法来完成翻译
{
if (lbxFrom.SelectedItem == null || lbxTo.SelectedItem == null)
{
MessageBox.Show("请先选择语言","提示",MessageBoxButton.OK);
return;
}
if (this.txtOrign.Text.Trim().Length==0)
{
MessageBox.Show("请先输入要翻译的单词或语句","提示",MessageBoxButton.OK);
return;
}
else
{
Language from = lbxFrom.SelectedItem as Language;
Language to = lbxTo.SelectedItem as Language;
proxy.TranslateAsync(APP_ID, txtOrign.Text, from.Code, to.Code);
}
}
}
完整MainPage.xaml代码
x:Class="BingTranslatorService.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="30">源语言:</TextBlock>
<ListBox x:Name="lbxFrom" Height="160" Margin="0,10,0,10" Width="440" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontSize="30"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Name="txtOrign" Text="" FontSize="25" TextWrapping="Wrap" Height="160" HorizontalAlignment="Left" Width="440"/>
<TextBlock FontSize="30">目标语言:</TextBlock>
<ListBox x:Name="lbxTo" Height="160" Margin="0,10,0,10" Width="440" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontSize="30"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Name="btnTranslate" Content="翻译"
Click="btnTranslate_Click" />
<TextBox Name="txtTarget" IsReadOnly="True" FontSize="30" TextWrapping="Wrap" Height="160" HorizontalAlignment="Left" Width="440"/>
</StackPanel>
</ScrollViewer>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
</UserControl>
完整MainPage.csl代码
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;
namespace BingTranslatorService
{
public partial class MainPage : PhoneApplicationPage
{
private List<string> codes;
private List<string> names;
private List<Language> languages;
private BTS.LanguageServiceClient proxy;
//http://cn.bing.com/developers/createapp.aspx
private readonly string APP_ID = "3DAEE5B978BA031557E739EE1E2A68CB1FAD5909";
public MainPage()
{
InitializeComponent();
codes = new List<string>();
names = new List<string>();
languages = new List<Language>();
proxy = new BTS.LanguageServiceClient();
this.Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);
}
private void btnTranslate_Click(object sender, RoutedEventArgs e)
{
Translate();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
proxy.GetLanguagesForTranslateCompleted += new EventHandler<BTS.GetLanguagesForTranslateCompletedEventArgs>(proxy_GetLanguagesForTranslateCompleted);
proxy.GetLanguageNamesCompleted += new EventHandler<BTS.GetLanguageNamesCompletedEventArgs>(proxy_GetLanguageNamesCompleted);
proxy.TranslateCompleted += new EventHandler<BTS.TranslateCompletedEventArgs>(proxy_TranslateCompleted);
proxy.GetLanguagesForTranslateAsync(APP_ID);
}
public void proxy_GetLanguagesForTranslateCompleted(object sender, BTS.GetLanguagesForTranslateCompletedEventArgs e)
{
codes = e.Result.ToList();
proxy.GetLanguageNamesAsync(APP_ID, "en", e.Result);
}
public void proxy_GetLanguageNamesCompleted(object sender, BTS.GetLanguageNamesCompletedEventArgs e)
{
names = e.Result.ToList();
loadLanguages();
}
public void proxy_TranslateCompleted(object sender, BTS.TranslateCompletedEventArgs e)
{
txtTarget.Text = e.Result;
}
private void loadLanguages()
{
for (int index = 0; index < codes.Count; index++)
{
languages.Add(new Language
{
Code = codes[index],
Name = names[index]
});
}
lbxFrom.ItemsSource = languages;
lbxTo.ItemsSource = languages;
}
private void Translate()
{
if (lbxFrom.SelectedItem == null || lbxTo.SelectedItem == null)
{
MessageBox.Show("请先选择语言", "提示", MessageBoxButton.OK);
return;
}
if (this.txtOrign.Text.Trim().Length == 0)
{
MessageBox.Show("请先输入要翻译的单词或语句", "提示", MessageBoxButton.OK);
return;
}
else
{
Language from = lbxFrom.SelectedItem as Language;
Language to = lbxTo.SelectedItem as Language;
proxy.TranslateAsync(APP_ID, txtOrign.Text, from.Code, to.Code);
}
}
}
}
</UserControl>