zoukankan      html  css  js  c++  java
  • WPF实例:通过WebServices获取城市的天气情况

    1 远程WebServices:http://www.ayandy.com/Service.asmx 一般情况可以,有时会出现不能连接

    2 创建WPF项目WpfWeather,并且添加WebServices引用到项目中Service1,app.config文件内容

    有时候自动生成的会有另外一个binding endpoint ,导致出现错误,删除就好了

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        
    <system.serviceModel>
            
    <bindings>
                
    <basicHttpBinding>
                    
    <binding name="ServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
                        receiveTimeout
    ="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                        bypassProxyOnLocal
    ="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferSize
    ="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                        messageEncoding
    ="Text" textEncoding="utf-8" transferMode="Buffered"
                        useDefaultWebProxy
    ="true">
                        
    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead
    ="4096" maxNameTableCharCount="16384" />
                        
    <security mode="None">
                            
    <transport clientCredentialType="None" proxyCredentialType="None"
                                realm
    ="" />
                            
    <message clientCredentialType="UserName" algorithmSuite="Default" />
                        
    </security>
                    
    </binding>
                
    </basicHttpBinding>
            
    </bindings>
            
    <client>
                
    <endpoint address="http://www.ayandy.com/Service.asmx" binding="basicHttpBinding"
                    bindingConfiguration
    ="ServiceSoap" contract="Service1.ServiceSoap"
                    name
    ="ServiceSoap" />
            
    </client>
        
    </system.serviceModel>
    </configuration>

    3 设计xaml文件界面:

    <Window x:Class="WpfWeather.Window1"
        xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
        Title
    ="Window1" Height="300" Width="300" Loaded="Window_Loaded">
        
    <Window.Resources>
            
    <DataTemplate x:Key="ComboBoxTemplate">
                
    <Label Content="{Binding Path=Name}"></Label>
            
    </DataTemplate>
        
    </Window.Resources>
        
    <Grid>
            
    <Canvas Margin="18,16,16,19" Name="canvas1">
                
    <ComboBox Canvas.Left="0" Canvas.Top="11" Height="34" Width="138" ItemsSource="{Binding}"  Name="cbcity" SelectionChanged="cbcity_SelectionChanged">
                    
    <ComboBox.ItemTemplate>
                        
    <StaticResource  ResourceKey="ComboBoxTemplate"></StaticResource>
                    
    </ComboBox.ItemTemplate>
                
    </ComboBox>
                
    <TextBlock Canvas.Left="8" Canvas.Top="55" Height="161" Name="tbDetail" Width="228" TextWrapping="Wrap"/>
                
    <ComboBox Canvas.Left="153" Canvas.Top="11" Height="34" Name="cbdate" Width="91" SelectionChanged="cbdate_SelectionChanged">
                    
    <ComboBoxItem>Today</ComboBoxItem>
                    
    <ComboBoxItem>Tomorrow</ComboBoxItem>
                    
    <ComboBoxItem>AfterTomorrow</ComboBoxItem>
                
    </ComboBox>
            
    </Canvas>
        
    </Grid>
    </Window>

    4 后台代码,从WebServices取值和给页面赋值:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 WpfWeather.Service1;
    using System.Data;
    using System.IO;

    namespace WpfWeather
    {
        
    /// <summary>
        
    /// Interaction logic for Window1.xaml
        
    /// </summary>
        public partial class Window1 : Window
        {
            
    public Window1()
            {
                InitializeComponent();
            }

            
    private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                
    try
                {
                    
    if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/city.xml"))
                    {
                        DataSet dsCity 
    = new DataSet();
                        dsCity.ReadXml(AppDomain.CurrentDomain.BaseDirectory 
    + "/city.xml");
                        cbcity.DataContext 
    = dsCity.Tables[0];
                    }
                    
    else
                    {
                        ServiceSoapClient ssc 
    = new ServiceSoapClient();
                        
    string[] citys = ssc.getSupportCity("all");

                        DataTable dt 
    = new DataTable();
                        DataColumn dc 
    = new DataColumn("ID"typeof(System.String));
                        dt.Columns.Add(dc);
                        dc 
    = new DataColumn("Name"typeof(System.String));
                        dt.Columns.Add(dc);
                        
    for (int i = 0; i < citys.Length; i++)
                        {
                            dt.Rows.Add(
    new object[] { i, citys[i] });
                        }
                        DataSet ds 
    = new DataSet();
                        ds.Tables.Add(dt);
                        ds.WriteXml(AppDomain.CurrentDomain.BaseDirectory 
    + "/city.xml");
                        cbcity.DataContext 
    = dt;
                    }
                }
                
    catch (Exception ex)
                {
                    tbDetail.Inlines.Clear();
                    tbDetail.Inlines.Add(
    new Run(ex.ToString()));
                }

            }

            
    private void cbcity_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                tbDetail.Inlines.Clear();
                GetDetail();
            }

            
    private void GetDetail()
            {
                
    try
                {
                    
    string city = ((DataRowView)cbcity.SelectedItem).Row.ItemArray[1].ToString();
                    ServiceSoapClient ssc 
    = new ServiceSoapClient();
                    
    string[] weather = new string[8];
                    
    if (cbdate.SelectedIndex != -1)
                    {
                        
    string DayFlag = ((ComboBoxItem)cbdate.SelectedItem).Content.ToString();
                        
    switch (DayFlag)
                        {
                            
    case "Today":
                                weather 
    = ssc.getWeatherbyCityName(city, theDayFlagEnum.Today);
                                
    break;
                            
    case "Tomorrow":
                                weather 
    = ssc.getWeatherbyCityName(city, theDayFlagEnum.Tomorrow);
                                
    break;
                            
    case "AfterTomorrow":
                                weather 
    = ssc.getWeatherbyCityName(city, theDayFlagEnum.theDayafterTomorrow);
                                
    break;
                        }
                    }
                    
    else
                    {
                        weather 
    = ssc.getWeatherbyCityName(city, theDayFlagEnum.Today);
                    }
                    
    for (int i = 0; i < weather.Length; i++)
                    {
                        tbDetail.Inlines.Add(
    new Run(weather[i]));
                        
    if (i != weather.Length - 1)
                        {
                            tbDetail.Inlines.Add(
    new LineBreak());
                        }
                    }

                }
                
    catch (Exception ex)
                {
                    tbDetail.Inlines.Add(
    new Run(ex.ToString()));
                }
            }

            
    private void cbdate_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                tbDetail.Inlines.Clear();
                
    if (cbcity.SelectedIndex != -1)
                {
                    GetDetail(); 
                }
                
    else
                {
                    
    return;
                }
            }
        }
    }
  • 相关阅读:
    14.挂载硬件设备
    13.Linux文件存储系统
    12.su 命令与sudo 服务
    11.文件访问控制列表
    10. 文件权限与归属
    9. Linux用户身份和能力
    8. Linux重要的环境变量
    7. Linux命令行的通配符、转义字符
    6. Linux输入输出重定向
    5. Linux文件目录管理和打包压缩与搜索命令
  • 原文地址:https://www.cnblogs.com/xh831213/p/1700692.html
Copyright © 2011-2022 走看看