C#添加WebService服务实现天气预报

用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界面代码

<Window
        xmlns="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>

猜你喜欢

转载自blog.csdn.net/qq_38765502/article/details/86413856