当前位置: 移动技术网 > IT编程>移动开发>WP > Windows Phone 练习1 天气预报查询

Windows Phone 练习1 天气预报查询

2018年10月13日  | 移动技术网IT编程  | 我要评论

将军在上在线观看,菊醉华灯,9999小游戏

 

这是我在博客园的第一篇博文,自我纪念一下,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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网