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 voidwindow_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 voidButton_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 voidselectChange_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);}}}}