加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WPF调用WebService实现天气预报

发布时间:2020-12-16 23:55:36 所属栏目:安全 来源:网络整理
导读:之前经常听同事说起WebService这个东西,今天认真看了看查到一篇关于“Winform调用WebService实现天气预报”的文章。 wpf与Winform几乎一样。都是调用互联网上公开的WebServices( http://www.webxml.com.cn/WebServices/WeatherWebService.asmx )来实现天气

之前经常听同事说起WebService这个东西,今天认真看了看查到一篇关于“Winform调用WebService实现天气预报”的文章。

wpf与Winform几乎一样。都是调用互联网上公开的WebServices(http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)来实现天气预报,该天气预报 Web 服务,数据来源于中国气象局?http://www.cma.gov.cn/?,数据每2.5小时左右自动更新一次,准确可靠。包括 340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。

程序效果:

?

实现步骤:

1、引入Web服务。在VS中项目上右击→添加服务引用。

2、在弹出的添加服务引用窗口,录入web服务地址和引用后的命名空间。

3、布置WPF窗体界面,实现代码。核心代码如下:?

<Window x:Class="Wpf调用webservices.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF调用WebService实现天气预报" Height="404" Width="662">
    <Grid>
        <Label Content="输入城市名称:" Height="29" HorizontalAlignment="Left" Margin="12,12,0" Name="label1" VerticalAlignment="Top" Width="108" />
        <Label Content="显示天气" Height="29" HorizontalAlignment="Left" Margin="94,91,0" Name="label3" VerticalAlignment="Top" Width="209" />
        <Label Content="天气概况:" Height="29" HorizontalAlignment="Left" Margin="12,0" Name="label4" VerticalAlignment="Top" Width="76" />
        <Label Content="天气实况:" Height="29" HorizontalAlignment="Left" Margin="12,164,0" Name="label5" VerticalAlignment="Top" Width="76" />
        <TextBox Height="29" HorizontalAlignment="Left" Margin="139,0" Name="textBox1" VerticalAlignment="Top" Width="81" />
        <Button Content="查询" Height="28" HorizontalAlignment="Left" Margin="228,13,0" Name="button1" VerticalAlignment="Top" Width="48" Click="button1_Click" />
        <Image Height="29" HorizontalAlignment="Right" Margin="0,364,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="33" Source="/Wpf调用webservices;component/bin/Debug/image/1.gif" />
        <TextBox Height="71" HorizontalAlignment="Left" Margin="94,130" Name="textBox2" VerticalAlignment="Bottom" Width="363" TextWrapping="Wrap" />
    </Grid>
</Window>


?

 private void button1_Click(object sender,RoutedEventArgs e)
        {
            Weather.WeatherWebServiceSoapClient w = new Weather.WeatherWebServiceSoapClient("WeatherWebServiceSoap");            
            string[] s = new string[23];//声明string数组存放返回结果  
            string city = this.textBox1.Text.Trim();//获得文本框录入的查询城市  
            s = w.getWeatherbyCityName(city);     
            if (s[8] == "")
            {
                MessageBox.Show("输入错误,请重新输入","提示",MessageBoxButton.OK,MessageBoxImage.Information);
            }
            else
            {               
                this.label3.Content = s[1] + " " + s[6];
                textBox2.Text = s[10];
            }   
        }

4、天气图标可至【http://www.webxml.com.cn/images/weather.zip】下载。

5、Web服务的各方法参数直接访问【http://www.webxml.com.cn/WebServices/WeatherWebService.asmx】查询,从而实现其它丰富功能,如未来天气预报等。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读