コード例 #1
0
        private void refresh_from_xml(string xml_content, int temp_format)
        {
            WeatherHelper.WeatherHelper.CurrentConditions current = new WeatherHelper.WeatherHelper.CurrentConditions();
            try
            {
                XElement root = XElement.Parse(xml_content);
                IEnumerable<XElement> forecast_info = from c in root.Elements().Elements("forecast_information") select c;
                IEnumerable<XElement> current_conditions = from c in root.Elements().Elements("current_conditions").Elements() select c;
                IEnumerable<XElement> forecast_conditions = from c in root.Elements().Elements("forecast_conditions") select c;
                IEnumerable<XElement> error_check = from c in root.Elements().Elements("problem_cause") select c;
                if (error_check.Count() > 0)
                {
                    current_condition.Text = AppResources.error_xml_error;
                    return;
                }
                foreach (XElement cd in current_conditions)
                {
                    current.setByName(cd.Name.LocalName, cd.Attribute("data").Value);
                }
                string localDateString = forecast_info.Elements("forecast_date").First().Attribute("data").Value;
                //Debug.WriteLine(localDateString);
                string[] localDateSplit = localDateString.Split(new char[] {'-'});
                // parse the local date time according to forecast
                DateTime localDate = DateTime.Now;
                if (localDateSplit.Length == 3)
                {
                    int year = 0, month = 0, day = 0;
                    if (int.TryParse(localDateSplit[0], out year) && int.TryParse(localDateSplit[1], out month) && int.TryParse(localDateSplit[2], out day))
                    {
                        localDate = new DateTime(year, month, day);
                    }
                }

                // try to dynamically add new pivot page
                // includes today
                int lookahead_count = 4;
                create_pivot_items(lookahead_count, localDate);

                string current_temp_text = "";
                if (temp_format == WeatherHelper.WeatherHelper.FAREN)
                {
                    current_temp_text = current.temp_f + WeatherHelper.WeatherHelper.DEGREE;
                }
                else
                {
                    current_temp_text = current.temp_c + WeatherHelper.WeatherHelper.DEGREE;
                }
                current_temp.Text = current_temp_text;
                current_wind.Text = current.wind.Replace("Wind", AppResources.wind);
                current_humidity.Text = current.humidity.Replace("Humidity", AppResources.humidity);
                current_condition.Text = WeatherHelper.TranslationHelper.translateCondition(current.condition);
                var v = (Visibility)Resources["PhoneLightThemeVisibility"];
                string local_image_folder = "dark";
                if (v == Visibility.Visible)
                {
                    local_image_folder = "light";
                }
                Uri imageUri = new Uri(WeatherHelper.WeatherHelper.BASE_ICON_PATH + local_image_folder + "/" + WeatherHelper.WeatherHelper.getLocalImageName(current.icon), UriKind.Relative);
                string iconPath = WeatherHelper.WeatherHelper.BASE_ICON_PATH + "tile/" + WeatherHelper.WeatherHelper.getLocalImageName(current.icon);
                current_image.Source = new BitmapImage(imageUri);
                current_image.Height = 200;
                List<WeatherHelper.WeatherHelper.ForecastConditions> forecast_list = new List<WeatherHelper.WeatherHelper.ForecastConditions>();
                // update tile
                TileHelper.updateTile(iconPath, current_temp_text);
                foreach (XElement fc in forecast_conditions)
                {
                    IEnumerable<XElement> cond = fc.Elements();
                    WeatherHelper.WeatherHelper.ForecastConditions fc_temp = new WeatherHelper.WeatherHelper.ForecastConditions();
                    foreach (XElement cd in cond)
                    {
                        fc_temp.setByName(cd.Name.LocalName, cd.Attribute("data").Value);
                    }
                    forecast_list.Add(fc_temp);
                    foreach (PivotItem pi in appPivot.Items)
                    {
                        //Debug.WriteLine(pi.Header.ToString());
                        //Debug.WriteLine(getLocalDay(fc_temp.day_of_week.Substring(0, 3).ToLower()));
                        if (pi.Header.ToString() == getLocalDay(fc_temp.day_of_week.Substring(0, 3).ToLower()))
                        {
                            string today = localDate.DayOfWeek.ToString();
                            // debug
                            /*Debug.WriteLine(today);
                            Debug.WriteLine(fc_temp.day_of_week);*/
                            if (fc_temp.day_of_week.Substring(0, 3).ToLower() == today.Substring(0, 3).ToLower())
                            {
                                pi.Header = AppResources.day_of_week_today;
                            }
                            StackPanel sp = new StackPanel();
                            TextBlock tb_cond = new TextBlock();
                            TextBlock tb_high = new TextBlock();
                            TextBlock tb_low = new TextBlock();
                            tb_high.FontSize = 64;
                            tb_low.FontSize = 52;
                            tb_high.Foreground = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
                            tb_low.Foreground = Application.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
                            tb_cond.FontSize = 24;
                            tb_high.TextAlignment = TextAlignment.Center;
                            tb_low.TextAlignment = TextAlignment.Center;
                            tb_cond.TextAlignment = TextAlignment.Center;
                            Thickness tb_cond_margin = new Thickness();
                            Thickness tb_high_margin = new Thickness();
                            tb_cond_margin.Top = 25;
                            tb_high_margin.Top = 10;
                            tb_cond.Margin = tb_cond_margin;
                            tb_high.Margin = tb_high_margin;
                            if (temp_format == WeatherHelper.WeatherHelper.FAREN)
                            {
                                tb_high.Text = fc_temp.high + WeatherHelper.WeatherHelper.DEGREE;
                                tb_low.Text = fc_temp.low + WeatherHelper.WeatherHelper.DEGREE;
                            }
                            else
                            {
                                tb_high.Text = WeatherHelper.WeatherHelper.f_to_c(fc_temp.high) + WeatherHelper.WeatherHelper.DEGREE;
                                tb_low.Text = WeatherHelper.WeatherHelper.f_to_c(fc_temp.low) + WeatherHelper.WeatherHelper.DEGREE;
                            }
                            tb_cond.Text = WeatherHelper.TranslationHelper.translateCondition(fc_temp.condition);
                            Image tb_image = new Image();
                            Uri tb_uri = null;
                            if (fc_temp.icon != null)
                            {
                                tb_uri = new Uri(WeatherHelper.WeatherHelper.BASE_ICON_PATH + local_image_folder + "/" + WeatherHelper.WeatherHelper.getLocalImageName(fc_temp.icon), UriKind.Relative);
                            }
                            else
                            {
                                //tb_uri = new Uri(WeatherHelper.WeatherHelper.BASE_ICON_PATH + local_image_folder + "/" + WeatherHelper.WeatherHelper.getLocalImageNameByCondition(fc_temp.condition), UriKind.Relative);
                            }
                            if (tb_uri != null)
                            {
                                tb_image.Source = new BitmapImage(tb_uri);
                            }
                            tb_image.Height = 200;
                            sp.Children.Add(tb_image);
                            sp.Children.Add(tb_cond);
                            sp.Children.Add(tb_high);
                            sp.Children.Add(tb_low);
                            pi.Content = sp;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                current_condition.Text = AppResources.error_general_exception + " : " + e.Message;
            }
        }
コード例 #2
0
        private void execute_refresh(RestClient client, RestRequest request)
        {
            client.ExecuteAsync(request, (response) =>
            {
                if (response.ResponseStatus != ResponseStatus.Completed || response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    // fail silently, no network connection
                }
                else
                {
                    var resource = response.Content;

                    /*
                     * cache results
                     */
                    if (settings.Contains("lastUpdated"))
                    {
                        settings["lastUpdated"] = DateTime.Now;
                    }
                    else
                    {
                        settings.Add("lastUpdated", DateTime.Now);
                    }
                    using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("xmlCache.xml", FileMode.Create, FileAccess.Write, IsolatedStorageFile.GetUserStoreForApplication())))
                    {
                        writer.Write(resource);
                        writer.Close();
                    }

                    int temp_format = WeatherHelper.WeatherHelper.FAREN;
                    if (settings.Contains("tempFormat"))
                    {
                        temp_format = (int)settings["tempFormat"];
                    }
                    WeatherHelper.WeatherHelper.CurrentConditions current = new WeatherHelper.WeatherHelper.CurrentConditions();
                    IEnumerable<XElement> current_conditions = from c in XElement.Parse(resource).Elements().Elements("current_conditions").Elements() select c;
                    IEnumerable<XElement> forecast_conditions = from c in XElement.Parse(resource).Elements().Elements("forecast_conditions") select c;
                    IEnumerable<XElement> error_check = from c in XElement.Parse(resource).Elements().Elements("problem_cause") select c;
                    if (error_check.Count() > 0)
                    {
                        NotifyComplete();
                    }
                    foreach (XElement cd in current_conditions)
                    {
                        current.setByName(cd.Name.LocalName, cd.Attribute("data").Value);
                    }
                    string iconPath = WeatherHelper.WeatherHelper.BASE_ICON_PATH + "tile/" + WeatherHelper.WeatherHelper.getLocalImageName(current.icon);
                    // update live tile
                    string current_temp_text = "";
                    if (temp_format == WeatherHelper.WeatherHelper.FAREN)
                    {
                        current_temp_text = current.temp_f + WeatherHelper.WeatherHelper.DEGREE;
                    }
                    else
                    {
                        current_temp_text = current.temp_c + WeatherHelper.WeatherHelper.DEGREE;
                    }
                    TileHelper.updateTile(iconPath, current_temp_text);
                    if (watcher != null)
                    {
                        watcher.Stop();
                    }
                }
                NotifyComplete();
            });
        }