当前位置:编程学习 > wap >>

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"))

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,