wp7 城市天气预报查询

    上次做了一个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://download.csdn.net/detail/huangyuancao/4290653

这里借鉴的是韩奕风的博客里面天气预报博客地址http://www.cnblogs.com/wildfeng/archive/2012/03/24/2415327.html

另外也参考博客http://blog.csdn.net/fengyun1989/article/details/7341703

感谢他们的帮助。。。

ps:做了2个小程序都是参考别人的,下一个打算思考自己做一个自己的东西吧。

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/newstart/p/2491288.html