新建手机应用程序项目
1、新建一个智能设备应用程序,在主面板上拖放需要的几个控件,其中timer是用来检查是否数据已经下载完毕的。
2、引用建好的WebService
3、WeatherService智能手机上运行的程序源代码
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
namespace WeatherService
{
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox_City;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Label txt_Weather1;
private System.Windows.Forms.Label txt_Date1;
private System.Windows.Forms.Label txt_Temp1;
private System.Windows.Forms.Label txt_WindPower1;
private System.Windows.Forms.Label txt_Temp2;
private System.Windows.Forms.Label txt_WindPower2;
private System.Windows.Forms.Label txt_Date2;
private System.Windows.Forms.Label txt_Weather2;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem_Menu;
private System.Windows.Forms.MenuItem menuItem_Exit;
private System.Windows.Forms.Label label_City;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
public string[] cityList = new string[]
{
"北京",
"上海",
"天津",
"石家庄",
"哈尔滨",
"沈阳",
"长春",
"太原",
"济南",
"郑州",
"天津",
"呼和浩特",
"西安",
"银川",
"兰州",
"西宁",
"乌鲁木齐",
"合肥",
"南昌",
"南京",
"杭州",
"武汉",
"长沙",
"广州",
"深圳",
"福州",
"厦门",
"南宁",
"桂林",
"海口",
"重庆",
"成都",
"贵阳",
"昆明",
"拉萨",
"香港",
"澳门",
"台北",
};
public MainForm()
{
InitializeComponent();
foreach( string x in cityList)
{
this.comboBox_City.Items.Add(x);
}
this.comboBox_City.SelectedIndex = 0;
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem_Menu = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.comboBox_City = new System.Windows.Forms.ComboBox();
this.label_City = new System.Windows.Forms.Label();
this.txt_Weather1 = new System.Windows.Forms.Label();
this.txt_Date1 = new System.Windows.Forms.Label();
this.txt_Temp1 = new System.Windows.Forms.Label();
this.txt_WindPower1 = new System.Windows.Forms.Label();
this.txt_Temp2 = new System.Windows.Forms.Label();
this.txt_WindPower2 = new System.Windows.Forms.Label();
this.txt_Date2 = new System.Windows.Forms.Label();
this.txt_Weather2 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer();
this.mainMenu1.MenuItems.Add(this.menuItem1);
this.mainMenu1.MenuItems.Add(this.menuItem_Menu);
this.menuItem1.Text = "确定";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
this.menuItem_Menu.MenuItems.Add(this.menuItem_Exit);
this.menuItem_Menu.MenuItems.Add(this.menuItem3);
this.menuItem_Menu.MenuItems.Add(this.menuItem2);
this.menuItem_Menu.Text = "菜单";
this.menuItem_Exit.Text = "退出";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);
this.menuItem3.Text = "-";
this.menuItem2.Text = "关于";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
this.comboBox_City.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.comboBox_City.Location = new System.Drawing.Point(52, 8);
this.comboBox_City.Size = new System.Drawing.Size(114, 22);
this.label_City.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Bold);
this.label_City.Location = new System.Drawing.Point(4, 12);
this.label_City.Size = new System.Drawing.Size(52, 16);
this.label_City.Text = "城市:";
this.label_City.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.txt_Weather1.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_Weather1.Location = new System.Drawing.Point(12, 50);
this.txt_Weather1.Size = new System.Drawing.Size(152, 14);
this.txt_Weather1.Text = "天气:";
this.txt_Date1.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_Date1.Location = new System.Drawing.Point(12, 34);
this.txt_Date1.Size = new System.Drawing.Size(152, 14);
this.txt_Date1.Text = "日期:";
this.txt_Temp1.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_Temp1.Location = new System.Drawing.Point(12, 68);
this.txt_Temp1.Size = new System.Drawing.Size(152, 14);
this.txt_Temp1.Text = "气温:";
this.txt_WindPower1.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_WindPower1.Location = new System.Drawing.Point(12, 84);
this.txt_WindPower1.Size = new System.Drawing.Size(152, 14);
this.txt_WindPower1.Text = "风力:";
this.txt_Temp2.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_Temp2.Location = new System.Drawing.Point(12, 140);
this.txt_Temp2.Size = new System.Drawing.Size(152, 14);
this.txt_Temp2.Text = "气温:";
this.txt_WindPower2.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_WindPower2.Location = new System.Drawing.Point(12, 156);
this.txt_WindPower2.Size = new System.Drawing.Size(152, 14);
this.txt_WindPower2.Text = "风力:";
this.txt_Date2.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_Date2.Location = new System.Drawing.Point(12, 108);
this.txt_Date2.Size = new System.Drawing.Size(152, 14);
this.txt_Date2.Text = "日期:";
this.txt_Weather2.Font = new System.Drawing.Font("Nina", 9F, System.Drawing.FontStyle.Regular);
this.txt_Weather2.Location = new System.Drawing.Point(12, 124);
this.txt_Weather2.Size = new System.Drawing.Size(152, 14);
this.txt_Weather2.Text = "天气:";
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.Controls.Add(this.comboBox_City);
this.Controls.Add(this.txt_Date1);
this.Controls.Add(this.txt_Weather1);
this.Controls.Add(this.label_City);
this.Controls.Add(this.txt_Temp1);
this.Controls.Add(this.txt_WindPower1);
this.Controls.Add(this.txt_Temp2);
this.Controls.Add(this.txt_WindPower2);
this.Controls.Add(this.txt_Date2);
this.Controls.Add(this.txt_Weather2);
this.Menu = this.mainMenu1;
this.Text = "天气预报";
}
#endregion
static void Main()
{
Application.Run(new MainForm());
}
private void menuItem_Exit_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
SearchWeather(this.comboBox_City.Text);
}
private string[] myString = new string[]
{
"日期: ",
"温度: ",
"天气: ",
"风力: ",
};
public void SearchWeather(string cityName)
{
if( this.txt_Date1.Text != myString[0] )
{
ReSelect();
}
this.Text = "正在下载...";
this.timer1.Enabled = true;
WeatherService.Weather weather = new WeatherService.Weather();
DataSet myDs = weather.GetWeatherDataSet(cityName);
this.txt_Date1.Text = myString[0] + myDs.Tables[0].Rows[0]["Date1"].ToString();
this.txt_Temp1.Text = myString[1] + myDs.Tables[0].Rows[0]["Temp1"].ToString();
this.txt_Weather1.Text = myString[2] + myDs.Tables[0].Rows[0]["Weather1"].ToString();
this.txt_WindPower1.Text = myString[3] + myDs.Tables[0].Rows[0]["WindPower1"].ToString();
this.txt_Date2.Text = myString[0] + myDs.Tables[0].Rows[0]["Date2"].ToString();
this.txt_Temp2.Text = myString[1] + myDs.Tables[0].Rows[0]["Temp2"].ToString();
this.txt_Weather2.Text = myString[2] + myDs.Tables[0].Rows[0]["Weather2"].ToString();
this.txt_WindPower2.Text = myString[3] + myDs.Tables[0].Rows[0]["WindPower2"].ToString();
}
private void ReSelect()
{
this.txt_Date1.Text = myString[0];
this.txt_Temp1.Text = myString[1];
this.txt_Weather1.Text = myString[2];
this.txt_WindPower1.Text = myString[3];
this.txt_Date2.Text = myString[0];
this.txt_Temp2.Text = myString[1];
this.txt_Weather2.Text = myString[2];
this.txt_WindPower2.Text = myString[3];
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if( this.txt_Date1.Text.IndexOf("月") != -1)
{
this.timer1.Enabled = false;
this.Text = "天气预报";
}
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Text = "作者:匿名";
System.Threading.Thread.Sleep(3000);
this.Text = "天气预报";
}
}
}