天气预报源代码

先上效果图

145236789

在这里更正一下,黑天的天气预报界面截图的时候,当时操作系统的时间是错误的,导致模拟器里显示2008年。

天气预报是我学WindowsPhone7以来的第一个作品。用的是谷歌的api。功能一个下午就写完了,还是这个界面的设计加P图费了我不少的时间。这个应用已经通过了微软的审核。

下载地址:

http://115.com/file/e7b668w9# 
Weather.xap

有什么问题请联系QQ:29992379 

由于界面过于复杂,为了实现高效的重用性,我使用了用户自定义控件。通过后台对象的实例化生成的界面。没有办法把所有的代码都展现出来。

   1:  // 应用程序启动(例如,从“开始”菜单启动)时执行的代码
   2:          // 此代码在重新激活应用程序时不执行
   3:          private void Application_Launching(object sender, LaunchingEventArgs e)
   4:          {
   5:              if (!IsolatedStorageSettings.ApplicationSettings.Contains("City"))
   6:              {
   7:                  List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
   8:                  IsolatedStorageSettings.ApplicationSettings["City"] = citys;
   9:                  IsolatedStorageSettings.ApplicationSettings.Save();
  10:              }
  11:          }
   1:  List<CityWeatherInfo> citys = new List<CityWeatherInfo>();

我使用的是IsolatedStorageSettings.ApplicationSettings存储的城市信息。下面为添加城市信息的代码。

   1:  //添加城市
   2:          private void AddCityButton_Click(object sender, RoutedEventArgs e)
   3:          {
   4:              
   5:              if ((citys.Where(list => list.CityName == CityNameTextBox.Text).ToList()).Count == 0)
   6:              {
   7:                  citys.Add(new CityWeatherInfo() { CityGuid = Guid.NewGuid().ToString(), CityName = CityNameTextBox.Text });
   8:                  IsolatedStorageSettings.ApplicationSettings["City"] = citys;
   9:                  IsolatedStorageSettings.ApplicationSettings.Save();
  10:              }
  11:              IsolatedStorageSettings.ApplicationSettings["City"] = citys;
  12:              IsolatedStorageSettings.ApplicationSettings.Save();
  13:              NavigationService.Navigate(new Uri("/Loading.xaml?cityName=" + CityNameTextBox.Text+"&AndGoPage=MainPage", UriKind.RelativeOrAbsolute));
  14:          }

 

   1:  //删除城市
   2:          private void linkButtonDel_Click(object sender, RoutedEventArgs e)
   3:          {
   4:              for (int i = 0; i < citys.Count; i++)
   5:              {
   6:                  if (citys[i].CityGuid == ((HyperlinkButton)sender).Tag.ToString())
   7:                  {
   8:                      citys.RemoveAt(i);
   9:                  }
  10:              }
  11:              IsolatedStorageSettings.ApplicationSettings["City"] = citys;
  12:              IsolatedStorageSettings.ApplicationSettings.Save();
  13:              BindData();
  14:          }

先上获取天气信息的代码,我自己写了个获取谷歌天气信息的工具类,生成了DLL然后引用进来使用的。

image

给城市赋天气信息

   1:   public partial class Loading : PhoneApplicationPage
   2:      {
   3:          List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
   4:          WebClient client = new WebClient();
   5:          string cityName = "济南";
   6:          public Loading()
   7:          {
   8:              InitializeComponent();
   9:              citys = IsolatedStorageSettings.ApplicationSettings["City"] as List<CityWeatherInfo>;
  10:          }
  11:          private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
  12:          {
  13:              LoadingData();
  14:          }
  15:          string AndGoPage = "MainPage";
  16:          public void LoadingData()
  17:          {
  18:              AndGoPage = NavigationContext.QueryString["AndGoPage"];
  19:              if (NavigationContext.QueryString.ContainsKey("cityName"))
  20:              {
  21:                  cityName = NavigationContext.QueryString["cityName"];
  22:              }
  23:              client.OpenReadAsync(new Uri("http://www.google.com/ig/api?hl=zh-cn&oe=utf8&weather=" + cityName, UriKind.RelativeOrAbsolute));
  24:              client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
  25:          }
  26:   
  27:          void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
  28:          {
  29:              XElement xmlWeather;
  30:              try
  31:              {
  32:                  xmlWeather = XElement.Load(e.Result);
  33:              }
  34:              catch (Exception)
  35:              {
  36:                  MessageBox.Show("获取城市信息失败");
  37:                  NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
  38:                  return;
  39:              }
  40:              foreach (var item in citys)
  41:              {
  42:                  if (item.CityName == cityName)
  43:                  {
  44:                      item.TodayIcon = GoogleWeatherHelper.GetTodayIcon(xmlWeather);
  45:                      item.LastUpdateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  46:                      item.Humidity = GoogleWeatherHelper.GetHumidity(xmlWeather);
  47:                      try
  48:                      {
  49:                          item.WindCondition = GoogleWeatherHelper.GetWindCondition(xmlWeather);
  50:                      }
  51:                      catch (Exception)
  52:                      {
  53:                          item.WindCondition = "未知";
  54:                      }
  55:                      item.TodayWeek = GoogleWeatherHelper.GetTodayWeek(xmlWeather);
  56:                      item.TodayIcon = GoogleWeatherHelper.GetTodayIcon(xmlWeather);
  57:                      item.TodayLow = GoogleWeatherHelper.GetTodayLow(xmlWeather);
  58:                      item.TodayHight = GoogleWeatherHelper.GetTodayHight(xmlWeather);
  59:                      item.TodayCondition = GoogleWeatherHelper.GetTodayCondition(xmlWeather);
  60:   
  61:                      item.TomorrowWeek = GoogleWeatherHelper.GetTomorrowWeek(xmlWeather);
  62:                      item.TomorrowIcon = GoogleWeatherHelper.GetTomorrowIcon(xmlWeather);
  63:                      item.TomorrowLow = GoogleWeatherHelper.GetTomorrowLow(xmlWeather);
  64:                      item.TomorrowHight = GoogleWeatherHelper.GetTomorrowHight(xmlWeather);
  65:                      item.TomorrowCondition = GoogleWeatherHelper.GetTomorrowCondition(xmlWeather);
  66:   
  67:                      item.HouTianWeek = GoogleWeatherHelper.GetHouTianWeek(xmlWeather);
  68:                      item.HouTianIcon = GoogleWeatherHelper.GetHouTianIcon(xmlWeather);
  69:                      item.HouTianLow = GoogleWeatherHelper.GetHouTianLow(xmlWeather);
  70:                      item.HouTianHight = GoogleWeatherHelper.GetHouTianHight(xmlWeather);
  71:                      item.HouTianCondition = GoogleWeatherHelper.GetHouTianCondition(xmlWeather);
  72:   
  73:                      item.DaHouTianWeek = GoogleWeatherHelper.GetDaHouTianWeek(xmlWeather);
  74:                      item.DaHouTianIcon = GoogleWeatherHelper.GetDaHouTianIcon(xmlWeather);
  75:                      item.DaHouTianLow = GoogleWeatherHelper.GetDaHouTianLow(xmlWeather);
  76:                      item.DaHouTianHight = GoogleWeatherHelper.GetDaHouTianHight(xmlWeather);
  77:                      item.DaHouTianCondition = GoogleWeatherHelper.GetDaHouTianCondition(xmlWeather);
  78:                  }
  79:              }
  80:              IsolatedStorageSettings.ApplicationSettings["City"] = citys;
  81:              IsolatedStorageSettings.ApplicationSettings.Save();
  82:              NavigationService.Navigate(new Uri("/" + AndGoPage + ".xaml?cityName=" + cityName, UriKind.RelativeOrAbsolute));
  83:          }
  84:      }

 

首页显示城市列表和城市精简天气信息的实现代码

   1:  public partial class MainPage : PhoneApplicationPage
   2:      {
   3:          List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
   4:          public MainPage()
   5:          {
   6:              InitializeComponent();
   7:              citys = IsolatedStorageSettings.ApplicationSettings["City"] as List<CityWeatherInfo>;
   8:              BingUI();
   9:          }
  10:   
  11:          private void BingUI()
  12:          {
  13:              string DayOrNight;
  14:              int hour = DateTime.Now.Hour;
  15:              ImageBrush img = new ImageBrush();
  16:              DayOrNight = TimeTools.GetDayorNight();
  17:              img.ImageSource = new BitmapImage(new Uri("/Images/Back/" + DayOrNight + ".jpg", UriKind.RelativeOrAbsolute));
  18:   
  19:              LayoutRoot.Background = img;
  20:              foreach (var item in citys)
  21:              {
  22:                  AddCity(item.CityName, item.TodayLow + "℃~" + item.TodayHight + "℃", "/Images/Forecasts/" + DayOrNight + "/" + item.TodayIcon + ".png");
  23:              }
  24:          }
  25:   
  26:          private void ApplicationBarIconButton_Click(object sender, EventArgs e)
  27:          {
  28:              NavigationService.Navigate(new Uri("/CityListEdit.xaml", UriKind.RelativeOrAbsolute));
  29:          }
  30:   
  31:          private void AddCity(string cityName, string cityTemperature, string WeatherIconPath) 
  32:          {
  33:              CityTileData cityData = new CityTileData();
  34:              cityData.cityTemperature = cityTemperature;
  35:              cityData.cityWeatherIcon = WeatherIconPath;
  36:              CityTile city = new CityTile();
  37:              city.DataContext = cityData;
  38:              city.cityName.Content = cityName;
  39:              city.Width = 184;
  40:              city.Height = 105;
  41:              city.Margin = new Thickness(15, 10, 15, 10);
  42:              wrapPanelCityList.Children.Add(city);
  43:              city.cityName.Click += new RoutedEventHandler(cityName_Click);
  44:          }
  45:   
  46:          void cityName_Click(object sender, RoutedEventArgs e)
  47:          {
  48:              NavigationService.Navigate(new Uri("/Loading.xaml?cityName=" + ((Button)sender).Content + "&AndGoPage=WeatherView", UriKind.RelativeOrAbsolute));
  49:          }
  50:   
  51:          protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
  52:          {
  53:              e.Cancel = true;
  54:              if (MessageBox.Show("", "确定要退出程序吗", MessageBoxButton.OKCancel)==MessageBoxResult.OK) 
  55:              {
  56:                  App.Quit();
  57:              }
  58:              base.OnBackKeyPress(e);
  59:          }
  60:   
  61:          private void ApplicationBarIconButton2_Click(object sender, EventArgs e)
  62:          {
  63:              NavigationService.Navigate(new Uri("/About.xaml", UriKind.RelativeOrAbsolute));
  64:          }
  65:      }

 

单个城市的详细天气信息以及未来4天的天气情况实现代码。

   1:  public partial class WeatherView : PhoneApplicationPage
   2:      {
   3:          List<CityWeatherInfo> citys = new List<CityWeatherInfo>();
   4:          public WeatherView()
   5:          {
   6:              InitializeComponent();
   7:              citys = IsolatedStorageSettings.ApplicationSettings["City"] as List<CityWeatherInfo>;
   8:          }
   9:          string cityName = "济南";
  10:          string TodayIcon = "";
  11:          string TodayInfo = "未知";
  12:          private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
  13:          {
  14:              if (NavigationContext.QueryString.ContainsKey("cityName"))
  15:              {
  16:                  cityName = NavigationContext.QueryString["cityName"];
  17:              }
  18:              BingUI();
  19:              BingData();
  20:          }
  21:          string DayOrNight;
  22:          private void BingData() 
  23:          {
  24:              foreach (var item in citys)
  25:              {
  26:                  if (item.CityName==cityName)
  27:                  {
  28:                      imgWeather.Source = new BitmapImage(new Uri("/Images/Weathericon/" + DayOrNight + "/" + item.TodayIcon + ".png", UriKind.RelativeOrAbsolute));
  29:   
  30:                      txtTodayTemperature.Text = item.TodayLow + "℃~" + item.TodayHight + "℃";
  31:                      txtCityName.Text = item.CityName;
  32:                      txtLastUpdateTime.Text = item.LastUpdateTime;
  33:                      TodayInfo = item.TodayCondition;
  34:                      txtTodayInfo.Text = "今日天气实况:" + item.TodayCondition + ";气温:" + txtTodayTemperature.Text + ";" + item.WindCondition + ";" + item.Humidity;
  35:                      TodayIcon = item.TodayIcon;
  36:                      forecastTile1.WhichDay = item.TodayWeek;
  37:                      forecastTile1.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.TodayIcon + ".png", UriKind.RelativeOrAbsolute));
  38:                      forecastTile1.Temperature = item.TodayLow + "°/" + item.TodayHight + "°";
  39:   
  40:                      forecastTile2.WhichDay = item.TomorrowWeek;
  41:                      forecastTile2.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.TomorrowIcon + ".png", UriKind.RelativeOrAbsolute));
  42:                      forecastTile2.Temperature = item.TomorrowLow + "°/" + item.TomorrowHight + "°";
  43:   
  44:                      forecastTile3.WhichDay = item.HouTianWeek;
  45:                      forecastTile3.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.HouTianIcon + ".png", UriKind.RelativeOrAbsolute));
  46:                      forecastTile3.Temperature = item.HouTianLow + "°/" + item.HouTianHight + "°";
  47:   
  48:                      forecastTile4.WhichDay = item.DaHouTianWeek;
  49:                      forecastTile4.Weathericon = new BitmapImage(new Uri("/Images/Forecasts/" + DayOrNight + "/" + item.DaHouTianIcon + ".png", UriKind.RelativeOrAbsolute));
  50:                      forecastTile4.Temperature = item.DaHouTianLow + "°/" + item.DaHouTianHight + "°";
  51:                  }
  52:              }
  53:          }
  54:   
  55:          private void BingUI() 
  56:          {
  57:              int hour = DateTime.Now.Hour;
  58:              ImageBrush img = new ImageBrush();
  59:              DayOrNight = TimeTools.GetDayorNight();
  60:              img.ImageSource = new BitmapImage(new Uri("/Images/Back/"+DayOrNight+".jpg", UriKind.RelativeOrAbsolute));
  61:              LayoutRoot.Background = img;
  62:          }
  63:   
  64:          protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
  65:          {
  66:              e.Cancel = true;
  67:              NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
  68:              base.OnBackKeyPress(e);
  69:          }
  70:   
  71:          private void ApplicationBarIconButton_Click(object sender, EventArgs e)
  72:          {
  73:              ChangeTile.ChangeTileByCityName(cityName, TodayIcon, TodayInfo, txtTodayTemperature.Text);
  74:          }
  75:      }

 

今天先更新到这个地方。尽请期待…..

原文中博客园地址:http://www.cnblogs.com/wildfeng/archive/2012/03/21/2410504.html

### 回答1: 如果您需要下载qt天气预报源代码,您可以进行以下步骤: 1. 首先,打开您的Web浏览器,并转到Qt官方网站(https://www.qt.io/)。 2. 在该网站上,点击右上角的"下载"按钮。 3. 在下载页面上,您可以选择不同的版本和平台。根据您的操作系统选择合适的版本(例如Windows、macOS或Linux)。 4. 选择您打算使用的版本后,点击下载按钮。 5. 下载完成后,打开下载的文件(可能是一个安装程序或一个压缩文件),根据安装向导的指示进行安装。 6. 安装完成后,打开Qt IDE(集成开发环境)。 7. 在Qt IDE中,可以创建新的Qt项目或打开现有的项目。 8. 根据您的需求,创建一个新的Qt项目,并选择适当的模板。 9. 在项目中,您可以使用Qt提供的各种类和库来开发天气预报应用程序。 10. 在您的项目中编写和实现天气预报相关的功能,并进行必要的测试和调试。 11. 最后,保存并导出您的项目,这样您就可以在其他设备上运行或分享代码。 通过以上步骤,您可以成功下载并使用Qt天气预报源代码。请注意,某些具体细节可能因不同的Qt版本和操作系统而有所不同。在遇到问题时,您可以参考Qt官方文档或社区中的帮助资。 ### 回答2: 您可以通过以下步骤下载qt天气预报源代码: 1. 首先,打开您的互联网浏览器。 2. 在搜索栏中输入“qt天气预报源代码下载”。点击搜索按钮。 3. 您将会看到很多相关搜索结果,选择一个可靠的网站,例如GitHub、CSDN等。 4. 进入该网站,并在搜索栏中输入“qt天气预报源代码”。 5. 点击搜索按钮,网站将会显示与您搜索相关的源代码项目。 6. 点击您选择的源代码项目,进入项目页面。 7. 在项目页面上,您可以找到源代码的下载选项,通常是一个或多个按钮、链接、或者绿色的“Download”按钮。 8. 点击下载按钮,选择保存源代码文件的位置。 9. 下载完成后,您可以打开下载的源代码文件,并使用适合qt开发环境的编辑器打开文件。 10. 接下来,您可以根据源代码的指引、注释等进行相关设置和修改。 11. 完成相关设置和修改后,您可以运行和调试qt天气预报应用程序。 希望以上的回答能够帮助到您,祝您下载成功并顺利进行qt天气预报应用程序的开发! ### 回答3: 要下载Qt天气预报源代码,可以按照以下步骤进行操作: 1. 首先,在网络上搜索Qt天气预报源代码的下载链接。可以尝试使用搜索引擎,或者直接在代码分享平台(如GitHub、码云)搜索相关关键词。 2. 找到适合的下载链接后,点击进入该页面,并查看相关信息,以确保源代码是适合您需要的版本和功能。 3. 在下载页面上,一般会提供下载按钮或链接。点击该按钮或链接开始下载源代码。根据网速和文件大小的不同,下载时间可能会有所不同。 4. 下载完成后,可以将源代码保存在本地的某个文件夹中。可以选择适当的文件夹路径,以便后续浏览和使用。 5. 解压源代码文件(如果是压缩包格式的)。可以使用常见的解压软件(如WinRAR、7-Zip)来完成解压操作。 6. 打开解压后的文件夹,可以看到源代码的相关文件和文件夹结构。其中,一般会包含项目文件(如.pro文件)和源代码文件(如.cpp、.h文件)。 7. 根据具体需求,可以使用Qt开发工具(如Qt Creator)打开项目文件,以便编辑和编译源代码。确保您已经安装了对应版本的Qt开发工具。 总之,下载Qt天气预报源代码需要在合适的网站上搜索下载链接,点击下载按钮或链接,保存到本地,并解压文件后,使用Qt开发工具打开相关文件进行编辑和编译。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值