这两天同事们问我有没有好一点的天气预报方面的控件啊?从网上搜罗了一番,发现提供这种服务的Webservice还是比较多的,但是貌似是没有这种控件的下载。实在是找不上的前提下我决定自己开发一个控件,又要支持简单的显示样式还要能通过事件返回天气预报详情数组,有兴趣的朋友可以看看这个东东!里面所使用的服务提供商为:上海思集信息科技有限公司是个免费的天气预报服务接口。有兴趣的朋友可以自己下源代码看看啊!
Coder
1
using
System;
2 using System.ComponentModel;
3 using System.Web.UI;
4 using System.Web.UI.WebControls;
5 using System.Net;
6 using System.IO;
7 using System.Xml;
8 using System.Text;
9 using System.Collections.Generic;
10
11 [assembly: WebResource( " WeatherReport.image.a_1.gif " , " image/gif " )]
12 namespace WeatherReport
13 {
14 /// <summary>
15 /// 天气预报控件
16 /// </summary>
17 [DefaultProperty( " Province " )]
18 [DefaultEvent( " OnWeatherReportLoadDataComplete " )]
19 [ToolboxData( " <{0}:WebWeatherReport ShowType=\ " Style\ " City=\ " 兰州\ " Width=\ " 100 % \ " runat=server></{0}:WebWeatherReport> " )]
20 [System.Drawing.ToolboxBitmap( typeof (WebWeatherReport), " ToolBar.ico " )]
21 public class WebWeatherReport : WebControl
22 {
23 /// <summary>
24 /// 注册服务读取以后的显示行为事件
25 /// </summary>
26 [Description( " 该事件配合控件呈现方式显示,一般在控件显示为字符串数组时回发结果数组 " )]
27 public event EventHandler < OnWeatherReportLoadDataCompleteEventArgs > OnWeatherReportLoadDataComplete;
28 /// <summary>
29 /// 获取或者设置控件的文本值
30 /// </summary>
31 [Bindable( true )]
32 [Category( " Appearance " )]
33 [DefaultValue( "" )]
34 [Localizable( true )]
35 public string Text
36 {
37 get
38 {
39 String s = (String)ViewState[ " Text " ];
40 return ((s == null ) ? String.Empty : s);
41 }
42
43 set
44 {
45 ViewState[ " Text " ] = value;
46 }
47 }
48
49 /// <summary>
50 /// 创建控件
51 /// </summary>
52 /// <param name="output"></param>
53 protected override void RenderContents(HtmlTextWriter output)
54 {
55 WebClient client = new WebClient();
56 Stream inStream = null ;
57 if (( this .City.Trim() == string .Empty))
58 {
59 inStream = client.OpenRead( " http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=兰州 " );
60 }
61 else
62 {
63 inStream = client.OpenRead( " http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName= " + this .City.ToString().Trim());
64 }
65 XmlDocument document = new XmlDocument();
66 document.Load(inStream);
67 XmlNodeList childNodes = document.DocumentElement.ChildNodes;
68 if (childNodes.Count != 0 && childNodes[ 0 ].InnerText != " 高速访问被限制! " )
69 {
70 switch ( this .ShowType)
71 {
72 case ControlsShowType.Style:
73 StringBuilder sb = new StringBuilder( " <div style=\ " margin: 0 auto;font - size:10pt;with: " + base.Width + " ;height: " + base.Height + " ;\ " > " );
74 sb.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >{0}天气预报,中央气象台{1}预报,</span> " , childNodes[ 1 ].InnerText, childNodes[ 4 ].InnerText));
75 sb.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >日期:{0}</span> <img src=\ " weather / { 1 }\ " />/<img src=\ " weather / { 2 }\ " /> " , childNodes[ 6 ].InnerText, childNodes[ 8 ].InnerText, childNodes[ 9 ].InnerText));
76 sb.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >今日气温:{0}</span></div> " , childNodes[ 5 ].InnerText.Replace( " ℃ " , " 度(℃) " )));
77 output.Write(sb.ToString());
78 break ;
79 case ControlsShowType.StringByEvents:
80 List < String > MyString = new List < string > ();
81 foreach (XmlNode nd in childNodes)
82 {
83 MyString.Add(nd.InnerText.ToString().Trim());
84 }
85 ShowMessage( new OnWeatherReportLoadDataCompleteEventArgs() { IsComplete = true , Result = MyString.ToArray() }, output);
86 break ;
87 default :
88 throw new Exception( " 未能正确选择显示类型! " );
89 case ControlsShowType.None:
90 StringBuilder sbs = new StringBuilder( " <div style=\ " margin: 0 auto;font - size:10pt;with: " + base.Width + " ;height: " + base.Height + " ;display:none;\ " > " );
91 sbs.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >{0}天气预报,中央气象台{1}预报</span> " , childNodes[ 1 ].InnerText, childNodes[ 4 ].InnerText));
92 sbs.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >日期:{0}</span> <img src=\ " weather / { 1 }\ " />/<img src=\ " weather / { 2 }\ " /> " , childNodes[ 6 ].InnerText, childNodes[ 8 ].InnerText, childNodes[ 9 ].InnerText));
93 sbs.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >今日气温:{0}</span></div> " , childNodes[ 5 ].InnerText.Replace( " ℃ " , " 度(℃) " )));
94 output.Write(sbs.ToString());
95 break ;
96 }
97 }
98 else
99 {
100 ShowMessage( new OnWeatherReportLoadDataCompleteEventArgs() { IsComplete = false , Result = null }, output);
101 output.Write( " 未能获取正确的天气预报预览,因为本控件使用免费的服务器接口,但不影响使用,详情请查看:<a href=\ " http: // www.webxml.com.cn/\"> http://www.webxml.com.cn/ </a>");
102 }
103 }
104 /// <summary>
105 /// 显示控件的外观行为
106 /// </summary>
107 /// <param name="e"></param>
108 /// <param name="T"></param>
109 protected void ShowMessage(OnWeatherReportLoadDataCompleteEventArgs e, HtmlTextWriter T)
110 {
111 if ( this .OnWeatherReportLoadDataComplete != null )
112 {
113 OnWeatherReportLoadDataComplete( this , e);
114 }
115 else
116 {
117 T.Write( " 要使用字符串形数组式返回的天气预报数据,必须在PageOnLoad事件中手动添加OnWebServiceLoaded事件! " );
118 }
119 }
120 /// <summary>
121 /// 枚举类型,获取控件显示类型
122 /// </summary>
123 public enum ControlsShowType
124 {
125 /// <summary>
126 /// 无样式
127 /// </summary>
128 None,
129 /// <summary>
130 /// 原有样式
131 /// </summary>
132 Style,
133 /// <summary>
134 /// 根据事件返回样式
135 /// </summary>
136 StringByEvents
137 }
138 /// <summary>
139 /// 获取或者设置控件的显示样式
140 /// </summary>
141 [Description( " 获取控件显示样式 " )]
142 [DefaultValue( " Style " )]
143 [Category( " Appearance " )]
144 [Browsable( true )]
145 public ControlsShowType ShowType { get ; set ; }
146 /// <summary>
147 /// 获取或者设置城市名称
148 /// </summary>
149 [Description( " 获取或者设置城市 " )]
150 [DefaultValue( " 北京 " )]
151 [Category( " Appearance " )]
152 [Browsable( true )]
153 public string City
154 {
155 get ;
156 set ;
157 }
158 /// <summary>
159 /// 获取或者设置是否是直辖市
160 /// </summary>
161 [Description( " 获取或者设置城市所在的省份 " )]
162 [DefaultValue( " 北京 " )]
163 [Category( " Appearance " )]
164 [Browsable( false )]
165 public bool IsMunicipalities
166 {
167 get ;
168 set ;
169 }
170 }
171 /// <summary>
172 /// 为字符数组显示类提供数据基类
173 /// </summary>
174 public class OnWeatherReportLoadDataCompleteEventArgs : EventArgs
175 {
176 /// <summary>
177 /// 默认构造函数
178 /// </summary>
179 public OnWeatherReportLoadDataCompleteEventArgs()
180 {
181 }
182 /// <summary>
183 /// 是否已经完成
184 /// </summary>
185 public Boolean IsComplete { get ; internal set ; }
186 /// <summary>
187 /// 数组返回值
188 /// </summary>
189 public string [] Result { get ; internal set ; }
190 }
191 }
192
2 using System.ComponentModel;
3 using System.Web.UI;
4 using System.Web.UI.WebControls;
5 using System.Net;
6 using System.IO;
7 using System.Xml;
8 using System.Text;
9 using System.Collections.Generic;
10
11 [assembly: WebResource( " WeatherReport.image.a_1.gif " , " image/gif " )]
12 namespace WeatherReport
13 {
14 /// <summary>
15 /// 天气预报控件
16 /// </summary>
17 [DefaultProperty( " Province " )]
18 [DefaultEvent( " OnWeatherReportLoadDataComplete " )]
19 [ToolboxData( " <{0}:WebWeatherReport ShowType=\ " Style\ " City=\ " 兰州\ " Width=\ " 100 % \ " runat=server></{0}:WebWeatherReport> " )]
20 [System.Drawing.ToolboxBitmap( typeof (WebWeatherReport), " ToolBar.ico " )]
21 public class WebWeatherReport : WebControl
22 {
23 /// <summary>
24 /// 注册服务读取以后的显示行为事件
25 /// </summary>
26 [Description( " 该事件配合控件呈现方式显示,一般在控件显示为字符串数组时回发结果数组 " )]
27 public event EventHandler < OnWeatherReportLoadDataCompleteEventArgs > OnWeatherReportLoadDataComplete;
28 /// <summary>
29 /// 获取或者设置控件的文本值
30 /// </summary>
31 [Bindable( true )]
32 [Category( " Appearance " )]
33 [DefaultValue( "" )]
34 [Localizable( true )]
35 public string Text
36 {
37 get
38 {
39 String s = (String)ViewState[ " Text " ];
40 return ((s == null ) ? String.Empty : s);
41 }
42
43 set
44 {
45 ViewState[ " Text " ] = value;
46 }
47 }
48
49 /// <summary>
50 /// 创建控件
51 /// </summary>
52 /// <param name="output"></param>
53 protected override void RenderContents(HtmlTextWriter output)
54 {
55 WebClient client = new WebClient();
56 Stream inStream = null ;
57 if (( this .City.Trim() == string .Empty))
58 {
59 inStream = client.OpenRead( " http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=兰州 " );
60 }
61 else
62 {
63 inStream = client.OpenRead( " http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName= " + this .City.ToString().Trim());
64 }
65 XmlDocument document = new XmlDocument();
66 document.Load(inStream);
67 XmlNodeList childNodes = document.DocumentElement.ChildNodes;
68 if (childNodes.Count != 0 && childNodes[ 0 ].InnerText != " 高速访问被限制! " )
69 {
70 switch ( this .ShowType)
71 {
72 case ControlsShowType.Style:
73 StringBuilder sb = new StringBuilder( " <div style=\ " margin: 0 auto;font - size:10pt;with: " + base.Width + " ;height: " + base.Height + " ;\ " > " );
74 sb.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >{0}天气预报,中央气象台{1}预报,</span> " , childNodes[ 1 ].InnerText, childNodes[ 4 ].InnerText));
75 sb.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >日期:{0}</span> <img src=\ " weather / { 1 }\ " />/<img src=\ " weather / { 2 }\ " /> " , childNodes[ 6 ].InnerText, childNodes[ 8 ].InnerText, childNodes[ 9 ].InnerText));
76 sb.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >今日气温:{0}</span></div> " , childNodes[ 5 ].InnerText.Replace( " ℃ " , " 度(℃) " )));
77 output.Write(sb.ToString());
78 break ;
79 case ControlsShowType.StringByEvents:
80 List < String > MyString = new List < string > ();
81 foreach (XmlNode nd in childNodes)
82 {
83 MyString.Add(nd.InnerText.ToString().Trim());
84 }
85 ShowMessage( new OnWeatherReportLoadDataCompleteEventArgs() { IsComplete = true , Result = MyString.ToArray() }, output);
86 break ;
87 default :
88 throw new Exception( " 未能正确选择显示类型! " );
89 case ControlsShowType.None:
90 StringBuilder sbs = new StringBuilder( " <div style=\ " margin: 0 auto;font - size:10pt;with: " + base.Width + " ;height: " + base.Height + " ;display:none;\ " > " );
91 sbs.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >{0}天气预报,中央气象台{1}预报</span> " , childNodes[ 1 ].InnerText, childNodes[ 4 ].InnerText));
92 sbs.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >日期:{0}</span> <img src=\ " weather / { 1 }\ " />/<img src=\ " weather / { 2 }\ " /> " , childNodes[ 6 ].InnerText, childNodes[ 8 ].InnerText, childNodes[ 9 ].InnerText));
93 sbs.Append(String.Format( " <span style=\ " color:#FF0000;font - family:微软雅黑;\ " >今日气温:{0}</span></div> " , childNodes[ 5 ].InnerText.Replace( " ℃ " , " 度(℃) " )));
94 output.Write(sbs.ToString());
95 break ;
96 }
97 }
98 else
99 {
100 ShowMessage( new OnWeatherReportLoadDataCompleteEventArgs() { IsComplete = false , Result = null }, output);
101 output.Write( " 未能获取正确的天气预报预览,因为本控件使用免费的服务器接口,但不影响使用,详情请查看:<a href=\ " http: // www.webxml.com.cn/\"> http://www.webxml.com.cn/ </a>");
102 }
103 }
104 /// <summary>
105 /// 显示控件的外观行为
106 /// </summary>
107 /// <param name="e"></param>
108 /// <param name="T"></param>
109 protected void ShowMessage(OnWeatherReportLoadDataCompleteEventArgs e, HtmlTextWriter T)
110 {
111 if ( this .OnWeatherReportLoadDataComplete != null )
112 {
113 OnWeatherReportLoadDataComplete( this , e);
114 }
115 else
116 {
117 T.Write( " 要使用字符串形数组式返回的天气预报数据,必须在PageOnLoad事件中手动添加OnWebServiceLoaded事件! " );
118 }
119 }
120 /// <summary>
121 /// 枚举类型,获取控件显示类型
122 /// </summary>
123 public enum ControlsShowType
124 {
125 /// <summary>
126 /// 无样式
127 /// </summary>
128 None,
129 /// <summary>
130 /// 原有样式
131 /// </summary>
132 Style,
133 /// <summary>
134 /// 根据事件返回样式
135 /// </summary>
136 StringByEvents
137 }
138 /// <summary>
139 /// 获取或者设置控件的显示样式
140 /// </summary>
141 [Description( " 获取控件显示样式 " )]
142 [DefaultValue( " Style " )]
143 [Category( " Appearance " )]
144 [Browsable( true )]
145 public ControlsShowType ShowType { get ; set ; }
146 /// <summary>
147 /// 获取或者设置城市名称
148 /// </summary>
149 [Description( " 获取或者设置城市 " )]
150 [DefaultValue( " 北京 " )]
151 [Category( " Appearance " )]
152 [Browsable( true )]
153 public string City
154 {
155 get ;
156 set ;
157 }
158 /// <summary>
159 /// 获取或者设置是否是直辖市
160 /// </summary>
161 [Description( " 获取或者设置城市所在的省份 " )]
162 [DefaultValue( " 北京 " )]
163 [Category( " Appearance " )]
164 [Browsable( false )]
165 public bool IsMunicipalities
166 {
167 get ;
168 set ;
169 }
170 }
171 /// <summary>
172 /// 为字符数组显示类提供数据基类
173 /// </summary>
174 public class OnWeatherReportLoadDataCompleteEventArgs : EventArgs
175 {
176 /// <summary>
177 /// 默认构造函数
178 /// </summary>
179 public OnWeatherReportLoadDataCompleteEventArgs()
180 {
181 }
182 /// <summary>
183 /// 是否已经完成
184 /// </summary>
185 public Boolean IsComplete { get ; internal set ; }
186 /// <summary>
187 /// 数组返回值
188 /// </summary>
189 public string [] Result { get ; internal set ; }
190 }
191 }
192
使用方法
1.打开VS添加新的选项卡然后点击选择项(如图1)
2.选择控件所在的文件夹如图:
3.选择完毕以后点击完成如图
4.然后双击进入页面即可使用了如图
目前这是初始化的样式,此方案中还包含一个服务提供者提供的天气预报的图片文件夹“/weather/”大家可以在帖子头部给出的连接上面去下载这个图片包。
这个控件支持的属性有:
The First Release Version:1.0.10330.2334 DownLoad