欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

Windows Phone 练习1 天气预报查询

来源: 开发者 投稿于  被查看 17127 次 评论:24

Windows Phone 练习1 天气预报查询


 

这是我在博客园的第一篇博文,自我纪念一下,O(∩_∩)O~

最近在学习Windows Phone相关知识,并做了一些练习,我想把这些练习记录下来,一来是我的成长记录,二来希望能对需要的人有所帮助。闲话少叙,言归正题。

今天的练习是天气预报查询,使用Web Service实现

地址:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

上图

运行界面:

 

\

 

文件列表:

 

\

 

MainPage.xaml文件代码:

 1 <phone:PhoneApplicationPage

 2     x:Class="Weather.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 is the root grid where all page content is placed-->

17     <Grid x:Name="LayoutRoot" Background="Transparent">

18         <Grid.RowDefinitions>

19             <RowDefinition Height="Auto"/>

20             <RowDefinition Height="*"/>

21         </Grid.RowDefinitions>

22

23         <!--TitlePanel contains the name of the application and page title-->

24         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

25             <TextBlock x:Name="ApplicationTitle" Text="天气查询" Style="{StaticResource PhoneTextNormalStyle}"/>

26         </StackPanel>

27

28         <!--ContentPanel - place additional content here-->

29         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

30             <StackPanel>

31                 <TextBlock Text="请输入城市或地区的名称:"></TextBlock>

32                 <TextBox Name="textBoxCity"></TextBox>

33                 <Button Name="buttonQuery" Content="查询" Width="100" Click="buttonQuery_Click"></Button>

34                 <TextBlock Name="textBlockResult" TextWrapping="Wrap"></TextBlock>

35             </StackPanel>

36         </Grid>

37     </Grid>

38 </phone:PhoneApplicationPage>

MainPage.xaml.cs文件代码:

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Net;

 5 using System.Windows;

 6 using System.Windows.Controls;

 7 using System.Windows.Documents;

 8 using System.Windows.Input;

 9 using System.Windows.Media;

10 using System.Windows.Media.Animation;

11 using System.Windows.Shapes;

12 using Microsoft.Phone.Controls;

13 using System.Text;

14 using System.IO.IsolatedStorage;

15

16 namespace Weather

17 {

18     public partial class MainPage : PhoneApplicationPage

19     {

20         //构造函数

21         public MainPage()

22         {

23             InitializeComponent();

24         }

25

26         //本练习使用的Web Service地址:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

27         //创建客户端实例

28         ServiceWeather.WeatherWSSoapClient client = new ServiceWeather.WeatherWSSoapClient();

29       

30         //要查询的城市或地区的名字

31         string keywords;

32

33         //当本页面成为活动页面时触发OnNavigatedTo事件

34         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)

35         {

36             base.OnNavigatedTo(e);

37             //注册当服务端发回数据时触发的事件

38             client.getWeatherCompleted += new EventHandler<ServiceWeather.getWeatherCompletedEventArgs>(client_getWeatherCompleted);

39

40             //在隔离存储空间中查找是否含有keywords项。这样做主要是为了方便用户,用户一般查询的是当地的天气,输入一次以后就不用输入了。

41             //当然如果用定位技术,定位手机所在的城市或地区会更好,这里先不涉及定位。

42             if (IsolatedStorageSettings.ApplicationSettings.Contains("keywords"))

43             {

44                 //如果含有keywords项,则把该项的值取出来

45                 keywords = IsolatedStorageSettings.ApplicationSettings["keywords"].ToString();

46                 //向服务端发出异步请求,查询天气信息

47                 client.getWeatherAsync(keywords, "");

48             }

49         }

50

51         //查询按钮点击事件

52         private void buttonQuery_Click(object sender, RoutedEventArgs e)

53         {

54             //把文本框textBoxCity中的值赋给keywords

55             keywords = textBoxCity.Text;

56             //向服务端发出异步请求,查询天气信息

57             client.getWeatherAsync(keywords, "");

58         }

59

60         //得到服务端发回的数据时触发该事件

61         void client_getWeatherCompleted(object sender, ServiceWeather.getWeatherCompletedEventArgs e)

62         {

63             //服务端返回的是一个字符串数组

64             //可以在这里测试http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather

65             string[] result = e.Result;

66             if (result.Length < 1)

67             {

68                 textBlockResult.Text = "没有此地的天气信息";

69             }

70             else if(result.Length==1)

71             {

72                 textBlockResult.Text=result[0];

73             }

74             else

75             {

76                 StringBuilder sb = new StringBuilder();

77                 sb.Append(result[1] + "\n");

78                 sb.Append(result[4] + "\n");

79                 sb.Append(result[5] + "\n\n");

80

81                 sb.Append("今天" + result[7] + "\n");

82                 sb.Append(result[8] + "\n");

83                 sb.Append(result[9] + "\n\n");

84

85                 sb.Append("明天" + result[12] + "\n");

86                 sb.Append(result[13] + "\n");

87                 sb.Append(result[14] + "\n\n");

88

89                 sb.Append("后天" + result[17] + "\n");

90                 sb.Append(result[18] + "\n");

91                 sb.Append(result[19] + "\n\n");

92

93                 textBlockResult.Text = sb.ToString();

94                 IsolatedStorageSettings.ApplicationSettings["keywords"] = keywords;

95                 IsolatedStorageSettings.ApplicationSettings.Save();

96             }

97         }

98     }

99 }

 

注释比较详细,我就不多言了。今天先到这里。歇歇。

作者 chengyujia

相关文章

    暂无相关文章
相关频道:

用户评论