Ejemplo n.º 1
0
        public async void AppBarButton_Click(object sender, RoutedEventArgs e)
        {
            response = new HttpResponseMessage();

            // if 'feedAddress' value changed the new URI must be tested --------------------------------
            // if the new 'feedAddress' doesn't work, 'feedStatus' informs the user about the incorrect input.

            //  feedStatus.Text = "Test if URI is valid";

            Uri resourceUri;

            if (!Uri.TryCreate(feedAddress.Trim(), UriKind.Absolute, out resourceUri))
            {
                //  feedStatus.Text = "Invalid URI, please re-enter a valid URI";
                return;
            }
            if (resourceUri.Scheme != "http" && resourceUri.Scheme != "https")
            {
                //  feedStatus.Text = "Only 'http' and 'https' schemes supported. Please re-enter URI";
                return;
            }
            // ---------- end of test---------------------------------------------------------------------

            string responseText;

            //feedStatus.Text = "Waiting for response ...";

            try
            {
                response = await httpClient.GetAsync(resourceUri);

                response.EnsureSuccessStatusCode();

                responseText = await response.Content.ReadAsStringAsync();

                //   statusPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                //// Need to convert int HResult to hex string
                //feedStatus.Text = "Error = " + ex.HResult.ToString("X") +
                //    "  Message: " + ex.Message;
                responseText = "";
            }
            //feedStatus.Text = response.StatusCode + " " + response.ReasonPhrase;

            // now 'responseText' contains the feed as a verified text.
            // next 'responseText' is converted as the rssItems class model definition to be displayed as a list

            List <rssItems> lstData    = new List <rssItems>();
            XElement        _xml       = XElement.Parse(responseText);
            XNamespace      media      = XNamespace.Get("http://search.yahoo.com/mrss/");
            XNamespace      dc         = "http://purl.org/dc/elements/1.1/";
            CultureInfo     dateformat = new CultureInfo("fr-FR");

            channelTitle.Text = _xml.Elements("channel").Elements("title").First().Value;


            foreach (XElement value in _xml.Elements("channel").Elements("item"))
            {
                rssItems _item = new rssItems();

                _item.Title = ShortenTitle(value.Element("title").Value);

                _item.Thumbnail = value.Element(media + "thumbnail") != null?value.Element(media + "thumbnail").Attribute("url").Value : "/Assets/Square71x71Logo.scale-240.png";


                _item.Link = value.Element("link").Value;

                _item.Desc = value.Element("description").Value;

                _item.Date = value.Element("pubDate").Value.Split(' ')[4];
                _item.Date = _item.Date.Remove(_item.Date.Length - 3);



                _item.Author = ShortenCreator(value.Element(dc + "creator").Value);

                lstData.Add(_item);
            }

            // lstRSS is bound to the lstData: the final result of the RSS parsing
            lstRSS.DataContext = lstData;
        }
Ejemplo n.º 2
0
        private async void AppBarButton_Click(object sender, RoutedEventArgs e)
        {
            response = new HttpResponseMessage();

            // if 'feedAddress' value changed the new URI must be tested --------------------------------
            // if the new 'feedAddress' doesn't work, 'feedStatus' informs the user about the incorrect input.

            feedStatus.Text = "Test if URI is valid";

            Uri resourceUri;
            if (!Uri.TryCreate(feedAddress.Trim(), UriKind.Absolute, out resourceUri))
            {
                feedStatus.Text = "Invalid URI, please re-enter a valid URI";
                return;
            }
            if (resourceUri.Scheme != "http" && resourceUri.Scheme != "https")
            {
                feedStatus.Text = "Only 'http' and 'https' schemes supported. Please re-enter URI";
                return;
            }
            // ---------- end of test---------------------------------------------------------------------

            string responseText;
            feedStatus.Text = "Waiting for response ...";

            try
            {
                response = await httpClient.GetAsync(resourceUri);

                response.EnsureSuccessStatusCode();

                responseText = await response.Content.ReadAsStringAsync();
                statusPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

            }
            catch (Exception ex)
            {
                // Need to convert int HResult to hex string
                feedStatus.Text = "Error = " + ex.HResult.ToString("X") +
                    "  Message: " + ex.Message;
                responseText = "";
            }
            feedStatus.Text = response.StatusCode + " " + response.ReasonPhrase;

            // now 'responseText' contains the feed as a verified text.
            // next 'responseText' is converted as the rssItems class model definition to be displayed as a list

            List<rssItems> lstData = new List<rssItems>();
            XElement _xml = XElement.Parse(responseText);
            foreach (XElement value in _xml.Elements("channel").Elements("item"))
            {
                rssItems _item = new rssItems();

                _item.Title = value.Element("title").Value;

                _item.Description = value.Element("description").Value;

                _item.Link = value.Element("link").Value;

                _item.Category = value.Element("category").Value;

                lstData.Add(_item);


            }

            // lstRSS is bound to the lstData: the final result of the RSS parsing
            lstRSS.DataContext = lstData;

        }