WP7中英文互翻译源码Demo
最终效果在WP7模拟器上
代码实现同样调用一个WebServices服务
前台代码:
1. <phone:PhoneApplicationPage
2. x:Class="WebCPServic.MainPage"
3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9. mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
10. FontFamily="{StaticResource PhoneFontFamilyNormal}"
11. FontSize="{StaticResource PhoneFontSizeNormal}"
12. Foreground="{StaticResource PhoneForegroundBrush}"
13. SupportedOrientations="Portrait" Orientation="Portrait"
14. shell:SystemTray.IsVisible="True">
15.
16. <!--LayoutRoot 是包含所有页面内容的根网格-->
17. <Grid x:Name="LayoutRoot" Background="Transparent">
18. <Grid.RowDefinitions>
19. <RowDefinition Height="Auto"/>
20. <RowDefinition Height="*"/>
21. </Grid.RowDefinitions>
22.
23. <!--TitlePanel 包含应用程序的名称和页标题-->
24. <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
25. <TextBlock x:Name="ApplicationTitle" Text="WP7开发者:dev.ruanman.net" Style="{StaticResource PhoneTextNormalStyle}"/>
26. <TextBlock x:Name="PageTitle" Text="中英文翻译" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
27. </StackPanel>
28.
29. <!--ContentPanel - 在此处放置其他内容-->
30. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
31. <TextBlock Height="49" HorizontalAlignment="Left" Margin="12,66,0,0" Name="des" Text="请输入你需要查询的英文或中文" VerticalAlignment="Top" Width="284" />
32. <TextBox Height="72" HorizontalAlignment="Left" Margin="6,106,0,0" Name="No" Text="dev.ruanman.net" VerticalAlignment="Top" Width="415" />
33. <Button Content="查询" Height="72" HorizontalAlignment="Left" Margin="12,184,0,0" Name="search" VerticalAlignment="Top" Width="247" Click="search_Click" />
34. <TextBlock Height="auto" TextAlignment="Center" HorizontalAlignment="Center" Margin="6,510,0,0" Name="hugwp" Text="www.ruanman.net" VerticalAlignment="Top" Width="444" />
35. <ListBox Height="242" HorizontalAlignment="Left" Margin="9,262,0,0" Name="listBox1" VerticalAlignment="Top" Width="441" />
36. </Grid>
37. </Grid>
38.
39. <!--演示 ApplicationBar 用法的示例代码-->
40. <!--<phone:PhoneApplicationPage.ApplicationBar>
41. <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
42. <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按钮 1"/>
43. <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按钮 2"/>
44. <shell:ApplicationBar.MenuItems>
45. <shell:ApplicationBarMenuItem Text="菜单项 1"/>
46. <shell:ApplicationBarMenuItem Text="菜单项 2"/>
47. </shell:ApplicationBar.MenuItems>
48. </shell:ApplicationBar>
49. </phone:PhoneApplicationPage.ApplicationBar>-->
50.
51. </phone:PhoneApplicationPage>
添加一个WebServices引用www.2cto.com
1. http://fy.webxml.com.cn/webservices/EnglishChinese.asmx
后台实现代码:
1. private void search_Click(object sender, RoutedEventArgs e)
2. {
3.
4. //实例化一个web service代理的对象
5.
6. EnglishChineseReference.EnglishChineseSoapClient proxy = new EnglishChineseReference.EnglishChineseSoapClient();
7.
8. //getMobileCodeInfo方法调用结束之后 触发的事件
9.
10. proxy.TranslatorStringCompleted += new EventHandler<EnglishChineseReference.TranslatorStringCompletedEventArgs>(proxy_GetIcpInfoByDomainCompleted);
11. //将调用信息包括方法名和参数加入到soap消息中通过http传送给web service服务端
12.
13. //这里对应的是调用了web service的getMobileCodeInfo方法
14.
15. proxy.TranslatorStringAsync(No.Text, "");
16.
17. }
18.
19. void proxy_GetIcpInfoByDomainCompleted(object sender, EnglishChineseReference.TranslatorStringCompletedEventArgs e)
20. {
21.
22. if (e.Error == null)
23. {
24. try
25. {
26. this.listBox1.ItemsSource = e.Result;
27.
28. }
29. catch (Exception)
30. {
31.
32. MessageBox.Show("网络出现问题,或者是手机号码错误");
33. }
34. }
35. else
36. {
37. MessageBox.Show("网络出现问题,或者是手机号码错误");
38. }
39. }
作者:迟浩东
上一篇: 一些语法在游戏开发中的应用