上次做了一个wp7的手机归属地查询,虽然找了参照来做但是还很有成就感,所以研究了2天天气预报做了半天做出效果图如下,不过这里说点感想:如果要开发wp7美工非常非常非常重要,不然就如网上所说的没有美工码农,都是苦逼。。。。废话了一堆上图(虽然很丑,但是也是我敲了半天赛)
这里可以直接输入城市也可以选择城市;后台代码:
/// <summary>
/// 选择城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnadd_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/CityPage.xaml", UriKind.RelativeOrAbsolute));
}
/// <summary>
/// 查询天气
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnseach_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/WheatherPage.xaml?cityName=" + textBox1.Text, UriKind.RelativeOrAbsolute));
}
/// <summary>
/// 返回城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("cityName"))
{
textBox1.Text = NavigationContext.QueryString["cityName"];
}
}
后台代码:这里初始化的时候直接把一个城市的xml文件读取到一个listbox显示
需要完善,还没实现动态加载。后台代码
//读取资源文件。文件为XML格式。这个文件的Building属性为Resource
StreamResourceInfo sri = Application.GetResourceStream(new Uri("/SearchWheather;component/citycode.xml",
UriKind.Relative));
//读取所以数据保存到String类型的result中
string result;
using (StreamReader sr = new StreamReader(sri.Stream))
{
result = sr.ReadToEnd();
}
//用XDocument类解析数据
XDocument doc = XDocument.Parse(result);
这里实现查询该城市最近4天的天气情况及温度情况等,用的是google的API;
public void LoadingData()
{
if (NavigationContext.QueryString.ContainsKey("cityName"))
{
cityName = NavigationContext.QueryString["cityName"];
}
client.OpenReadAsync(new Uri("http://www.google.com/ig/api?hl=zh-cn&oe=utf8&weather=" + cityName, UriKind.RelativeOrAbsolute));
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
}
本文转自夜&枫博客园博客,原文链接:http://www.cnblogs.com/newstart/archive/2012/05/09/2491288.html,如需转载请自行联系原作者