用C#做一个类似于上图的天气查询:

首先引用webservice服务

<?xml version="1.0" encoding="utf-8" ?>
<configuration><configSections><sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ><section name="WeatherService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /></sectionGroup></configSections><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /></startup><applicationSettings><WeatherService.Properties.Settings><setting name="WeatherService_Weather_WeatherWebService" serializeAs="String"><value>http://www.webxml.com.cn/WebServices/WeatherWebService.asmx</value></setting></WeatherService.Properties.Settings></applicationSettings>
</configuration>

然后创建接口:


using System.ServiceModel;namespace WeatherService
{interface Interface{[OperationContract]//得到天气具体情况string[] GetWeatherbyCityName(string cityname);[OperationContract]//根据省份获取对应的城市string[] GetSupportCity(string proviceName);[OperationContract]//获取省份string[] GetSupportProvince();}
}

实现接口


namespace WeatherService
{class WeatherServer : Interface{public string[] GetSupportCity(string proviceName){//获取服务对象  调用网站提供的方法Weather.WeatherWebService service = new Weather.WeatherWebService();return service.getSupportCity(proviceName);}public string[] GetSupportProvince(){Weather.WeatherWebService service = new Weather.WeatherWebService();return service.getSupportProvince();}public string[]  GetWeatherbyCityName(string cityname){Weather.WeatherWebService service = new Weather.WeatherWebService();string[] s = new string[23];s = service.getWeatherbyCityName(cityname);return s;}}

创建界面


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using System.Security.Policy;namespace WeatherService
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{ServiceHost host;public MainWindow(){InitializeComponent();}private void window_Loaded(object sender,RoutedEventArgs s){host = new ServiceHost(typeof(WeatherServer));host.Open();WeatherServer service = new WeatherServer();string[] province= service.GetSupportProvince();//下拉框绑定省份数据foreach(var item in province){this.comboBox.Items.Add(item);}}private void Button_Click(object sender, RoutedEventArgs e){string initcity = this.comboBox1.Text;int max = initcity.IndexOf("(");string city = initcity.Substring(0, max);WeatherServer service = new WeatherServer();string[] s = service.GetWeatherbyCityName(city);this.tb2.Text = s[1];this.tb3.Text = s[5];this.tb4.Text = s[6];this.tb5.Text = s[7];//得到图片路径string spath = @"C:\Users\43530\Documents\Visual Studio 2015\Projects\WeatherService\WeatherService\image\"+s[8];string spath1 = @"C:\Users\43530\Documents\Visual Studio 2015\Projects\WeatherService\WeatherService\image\" + s[9];this.tb6.Text =spath;this.tb7.Text = spath1;this.label4.Content = s[10] + "\n" + s[11];//this.button.Content = new Url(@" C:\Users\43530\Documents\Visual Studio 2015\Projects\WeatherService\WeatherService\image\"+s[9]);// @" C:\Users\43530\Documents\Visual Studio 2015\Projects\WeatherService\WeatherService\image\0.gif";//this.tb2 .Text= service.GetWeatherbyCityName(this.tb1.Text.ToString());}private void selectChange_SelectionChanged(object sender, SelectionChangedEventArgs e){WeatherServer service = new WeatherServer();string provicename= "";//如果选中this.comboBox1.Items.Clear();if (comboBox.SelectedIndex>-1){provicename = comboBox.Items[comboBox.SelectedIndex].ToString();//方法1}//得到相对应的城市绑定下拉框string[] city= service.GetSupportCity(provicename);foreach (var item in city){this.comboBox1.Items.Add(item);}}}
}

wpf界面代码

<Windowxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WeatherService"xmlns:Weather="clr-namespace:WeatherService.Weather" xmlns:Properties="clr-namespace:WeatherService.Properties" x:Class="WeatherService.MainWindow"mc:Ignorable="d"Title="天气预报" Height="350" Width="699" Loaded="window_Loaded"><Window.Background><SolidColorBrush Color="{DynamicResource {x:Static SystemColors.MenuColorKey}}"/></Window.Background><Grid RenderTransformOrigin="0.5,0.659" Margin="0,0,0,-0.2"><Grid.Background><SolidColorBrush Color="{DynamicResource {x:Static SystemColors.MenuColorKey}}"/></Grid.Background><Grid.ColumnDefinitions><ColumnDefinition Width="178*"/><ColumnDefinition Width="47*"/><ColumnDefinition Width="294*"/></Grid.ColumnDefinitions><Button Width="90" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left"  Margin="193.6,30,0,0" Content="查询" Click="Button_Click" Background="#FF00EDFF" Grid.Column="2" /><Canvas HorizontalAlignment="Left" Height="243" Margin="0,78,-0.2,0" VerticalAlignment="Top" Width="693" Grid.ColumnSpan="3" Background="#FFEAEAFF"><Label x:Name="label" Content="城市" Canvas.Left="25" Canvas.Top="23" RenderTransformOrigin="0.273,-0.125" Width="57" VerticalContentAlignment="Center" HorizontalContentAlignment="Left"/><TextBox x:Name="tb2" VerticalContentAlignment="Center" Height="30" Canvas.Left="87" TextWrapping="Wrap" Canvas.Top="26" Width="120" Background="#FFEAEAFF" /><Label x:Name="label1" Content="气温" Canvas.Left="25" Canvas.Top="75" Width="57" RenderTransformOrigin="-0.295,0.124"/><TextBox x:Name="tb3" VerticalContentAlignment="Center" Height="30" Canvas.Left="87" TextWrapping="Wrap" Canvas.Top="78" Width="120"  Background="#FFEAEAFF"  /><Label x:Name="label2" Content="概况" Canvas.Left="25" Canvas.Top="132" Width="57"/><TextBox x:Name="tb4" VerticalContentAlignment="Center" Height="30" Canvas.Left="87" TextWrapping="Wrap" Canvas.Top="134" Width="120" Background="#FFEAEAFF"/><Label x:Name="label3" Content="风况" Canvas.Left="25" Canvas.Top="190" Width="57"/><TextBox x:Name="tb5" VerticalContentAlignment="Center" Height="30" Canvas.Left="87" TextWrapping="Wrap"  Canvas.Top="190" Width="120" Background="#FFEAEAFF"/><TextBox x:Name="tb6" Height="23" Canvas.Left="391" TextWrapping="Wrap"  Canvas.Top="26" Width="120" Background="#FFEAEAFF" IsEnabled="False" Visibility="Hidden"></TextBox><Button x:Name="button" Canvas.Left="212" Canvas.Top="134" Width="22" Height="22" RenderTransformOrigin="0.519,0.465"><Button.Background><ImageBrush ImageSource="{Binding Text, ElementName=tb6}" Stretch="Fill"/></Button.Background></Button><Button x:Name="button1" Canvas.Left="239" Canvas.Top="134" Height="22" Width="22" RenderTransformOrigin="0.468,0.395"><Button.Background><ImageBrush ImageSource="{Binding Text, ElementName=tb7}" Stretch="Fill"/></Button.Background></Button><TextBox x:Name="tb7" Height="23" Canvas.Left="386" TextWrapping="Wrap" Text="TextBox" Canvas.Top="49" Width="120" Visibility="Hidden"/><Label x:Name="label4" Canvas.Left="272" Width="388" Height="243"/></Canvas><ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="147,38,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="selectChange_SelectionChanged" Grid.ColumnSpan="2"/><ComboBox x:Name="comboBox1" Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Right" Margin="0,38,285.8,0" VerticalAlignment="Top" Width="120"/><Label x:Name="label5" Content="请选择省份或者州:" HorizontalAlignment="Left" Margin="10,38,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.891,0.533" Width="119"/></Grid>
</Window>

C#添加WebService服务实现天气预报相关推荐

  1. Python调用WebService服务 查询天气预报

    文章目录 问题描述 安装 初试 修复Schema 天气预报封装 参考文献 问题描述 Python调用Webservice服务,如WebXml的免费Web服务中的城市天气预报 安装 使用suds库的分支 ...

  2. 免费天气预报的WebService服务接口

    一 首先找到一个免费天气预报的WebService服务接口 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 二 查看其 ...

  3. VS2008非托管c++访问webservice服务(以WeatherWS 天气服务 为例)

    在Visual Studio 2008以及以后版本中,微软停止了非托管C++的直接WebService引用.不过ATL Server代码已经托管到开源网站上,我们可以找到ATL Server的源代码, ...

  4. [置顶]WebService学习总结(4)——第三方webService服务调用

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据和查询国内手机号码归属地为 ...

  5. java调用webservice_WebService学习总结(四)——调用第三方提供的webService服务

    只为成功找方法,不为失败找借口! 互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气 ...

  6. 【客户下单】后台提供webservice服务接收数据

    [客户下单]后台提供webservice服务接收数据 在bos_fore系统中,添加OrderAction封装订单数据. 接下来调用webservice,将数据传递给bos_management系统. ...

  7. .NET创建WebService服务简单的例子

    Web service是一个基于可编程的web 的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务 WebService的特性有以下几点: 1.使用XML(标准通用标记语言)来作为数据交 ...

  8. gsoap搭建WebService服务

    WebService.soap.gsoap基本概念 WebService服务基本概念:就是一个应用程序,它向外界暴露出一个可以通过web进行调用的API,是分布式的服务组件.本质上就是要以标准的形式实 ...

  9. WebService学习总结——调用第三方提供的webService服务

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示,下面就以获取天气预报数据.气象中心的管理系统将收集 ...

最新文章

  1. 《货币是个什么东西》笔记
  2. MyBatis Plus——自定义配置——自定义属性——注入时间
  3. 好程序员web前端技术之CSS3过渡
  4. IOS 设备信息读取
  5. 科学家公布“寿命计算器”,快算一下你能活多久?
  6. 英特尔助力金山云带你畅游云端的游戏世界
  7. php将已有图片缩放尺寸,PHP图片重新采样及大小缩放-imagecopyresampled用法实例
  8. nyoj-673-悟空的难题(数组标记)
  9. java模块化发布选型_Java模块化开发
  10. 修改初始Manager QuerySets,重写Manager.get_query_set()函数之后,发现并没有按照我们指定的方法执行。...
  11. python股票_python股票 - 随笔分类 - 無碼 - 博客园
  12. 更深入的理解动态代理——一个真正让我理解动态代理应用价值的示例
  13. IIS5.1、IIS6.0、IIS7.5中安装配置MVC 3
  14. 不挖坑比努力填坑更值得
  15. Matlab报错 :“位置 x 处的索引超出数组边界”
  16. Word vba 替换
  17. python教你如何把自己的微信变成机器人
  18. 【软件工程】软件工程中应用的几种图辨析:系统流程图、数据流图、数据字典、实体联系图、状态转换图、层次方框图、Warnier图、IPO图、层次图、HIPO图、结构图、程序流程图、盒图、PAD图、判定表
  19. win10 dpc_watchdog_violation 蓝屏
  20. [QNX 自适应分区用户指南]12 APS开发实践

热门文章

  1. STM32模拟IIC驱动OLED屏 原理+源码
  2. MAX1480B中文数据手册
  3. 摄像无线传送服务器,如何将网络摄像头数据流传输到服务器?
  4. js如何让一个元素触发click点击事件
  5. 我的世界服务器反反作弊最新版,【服务器插件】【完全的反作弊】
  6. [附源码]java毕业设计企业记账系统
  7. 新手小白教程之 圈X-QuantumultX 某青极速版自动签到阅读
  8. python asyncio 异步 I/O - 实现并发http请求(asyncio + aiohttp)
  9. tomcat 服务器访问日志配置方法
  10. 神马笔记 版本2.11.0——歌词笔记