以下是我的代码
weatherForcast.cs
class weatherForcast
{
private WeatherWSSoapClient client;
private string cityName;
private string weatherResult;
public weatherForcast(string cn)
{
client = new WeatherWSSoapClient();
client.getWeatherCompleted += new EventHandler<getWeatherCompletedEventArgs>(client_getWeatherCompleted);
cityName = cn;
weatherResult = "";
}
public string getWeather()
{
client.getWeatherAsync(cityName, "");
return weatherResult;
}
public void client_getWeatherCompleted(object sender, getWeatherCompletedEventArgs e)
{
string[] result = e.Result;
if (result.Length < 1)
{
weatherResult = "没有此地的天气信息";
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append(result[1] + "\n");//城市名字
sb.Append(result[4] + "\n");//今日天气实况
sb.Append(result[5] + "\n\n");//空气质量、紫外线强度
sb.Append("今天" + result[7] + "\n");//日期、天气状况
sb.Append(result[8] + "\n");//气温
sb.Append(result[9] + "\n\n");//风向、风速
//sb.Append("明天" + result[12] + "\n");
//sb.Append(result[13] + "\n");
//sb.Append(result[14] + "\n\n");
//sb.Append("后天" + result[17] + "\n");
//sb.Append(result[18] + "\n");
//sb.Append(result[19] + "\n\n");
weatherResult = sb.ToString();
}
}
}
然后我在MainPage.xaml.cs中调用如下
weatherForcast wf = new weatherForcast("武汉");
string weather = wf.getWeather();