wp7查询天气

来源:互联网 发布:程序员编码规范 编辑:程序博客网 时间:2024/04/30 08:28
<Grid x:Name="LayoutRoot" Background="Transparent">     
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>


        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock FontSize="48" x:Name="ApplicationTitle" Text="天气查询" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>


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


        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 
            <StackPanel>
              <TextBlock FontSize="36" Text="请输入城市或地区的名称:"></TextBlock>
                <TextBox Name="textBoxCity" Width="439"></TextBox>
                <Button Name="buttonQuery" Content="查询" Width="407" Click="buttonQuery_Click"></Button>
                <TextBlock FontSize="35" Name="textBlockResult" TextWrapping="Wrap"></TextBlock>
         
            </StackPanel>

        

        </Grid>
 

    </Grid>


         //本练习使用的Web Service地址:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx 记住这里一定要添加引用
         //创建客户端实例
         ServiceWeather.WeatherWSSoapClient client = new ServiceWeather.WeatherWSSoapClient();
        
         //要查询的城市或地区的名字
         string keywords;
 
         //当本页面成为活动页面时触发OnNavigatedTo事件
         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
         {
             base.OnNavigatedTo(e);
             //注册当服务端发回数据时触发的事件
             client.getWeatherCompleted += new EventHandler<ServiceWeather.getWeatherCompletedEventArgs>(client_getWeatherCompleted);
 
             //在隔离存储空间中查找是否含有keywords项。这样做主要是为了方便用户,用户一般查询的是当地的天气,输入一次以后就不用输入了。
 //当然如果用定位技术,定位手机所在的城市或地区会更好,这里先不涉及定位。
             if (IsolatedStorageSettings.ApplicationSettings.Contains("keywords"))
             {
                 //如果含有keywords项,则把该项的值取出来
                 keywords = IsolatedStorageSettings.ApplicationSettings["keywords"].ToString();
                 //向服务端发出异步请求,查询天气信息
                 client.getWeatherAsync(keywords, "");
             }
         }
 
         //查询按钮点击事件
         private void buttonQuery_Click(object sender, RoutedEventArgs e)
         {
             //把文本框textBoxCity中的值赋给keywords
             keywords = textBoxCity.Text;
             //向服务端发出异步请求,查询天气信息
             client.getWeatherAsync(keywords, "");
         }
 
         //得到服务端发回的数据时触发该事件
         void client_getWeatherCompleted(object sender, ServiceWeather.getWeatherCompletedEventArgs e)
         {
             //服务端返回的是一个字符串数组
 //可以在这里测试 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather
             string[] result = e.Result;
             if (result.Length < 1)
             {
                 textBlockResult.Text = "没有此地的天气信息";
             }
             else if(result.Length==1)
             {
                 textBlockResult.Text=result[0];
             }
             else
             {
                 StringBuilder sb = new StringBuilder();
                 sb.Append(result[1] + "\n");
                 sb.Append(result[4] + "\n");
                 sb.Append(result[5] + "\n\n");
 
                 sb.Append("今天" + result[7] + "\n");
                 sb.Append(result[8] + "\n");
                 sb.Append(result[9] + "\n\n");
 
                 sb.Append("明天" + result[12] + "\n");
                 sb.Append(result[13] + "\n");
                 sb.Append(result[14] + "\n\n");
 
                 sb.Append("后天" + result[17] + "\n");
                 sb.Append(result[18] + "\n");
                 sb.Append(result[19] + "\n\n");
 
                 textBlockResult.Text = sb.ToString();
                 IsolatedStorageSettings.ApplicationSettings["keywords"] = keywords;
                 IsolatedStorageSettings.ApplicationSettings.Save();
             }
         }

原创粉丝点击